473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem calling method from parent of an object if object relayson template arguments of current template class?


Test case:

#include <boost/shared_ptr.hpp>

using namespace boost;

template <typename typ1> class cData {
public:
template <typename T> T As() { } // As
};
template <typename D, typename T>
class cTaggedData : public cData<D> {
public:
};

template <typename D, typename T>
class cAttr : public cTaggedData<D,T> {
public:
};

template <typename D, typename T>
class cNode : public cTaggedData<D,T> {
public:

shared_ptr< cAttr<D,T> > RetAttr() { }
template <typename X> X MyAs(T t, X& x) {
#line 100
shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
#line 200
shared_ptr< cAttr<D,T> > attr2; attr2->As<int>(); // FAILS
#line 300
shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
#line 400
shared_ptr< cAttr<D,T> > attr4; attr4->As<X>(); // my goal, FAILS
} // As

};
/*
gives:

x.cpp: In member function 'X cNode<D, T>::MyAs(T, X&)':
x.cpp:200: error: expected primary-expression before 'int'
x.cpp:200: error: expected `;' before 'int'
x.cpp:400: error: expected primary-expression before '>' token
x.cpp:400: error: expected primary-expression before ')' token
*/

int main() { }

How to fix thoes two errors? Especially the 400 one?

Mar 2 '06 #1
2 1580
* Rafał Maj Raf256:
Test case:

#include <boost/shared_ptr.hpp>

using namespace boost;

template <typename typ1> class cData {
public:
template <typename T> T As() { } // As
};
template <typename D, typename T>
class cTaggedData : public cData<D> {
public:
};

template <typename D, typename T>
class cAttr : public cTaggedData<D,T> {
public:
};

template <typename D, typename T>
class cNode : public cTaggedData<D,T> {
public:

shared_ptr< cAttr<D,T> > RetAttr() { }
template <typename X> X MyAs(T t, X& x) {
#line 100
shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
#line 200
shared_ptr< cAttr<D,T> > attr2; attr2->As<int>(); // FAILS
#line 300
shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
#line 400
shared_ptr< cAttr<D,T> > attr4; attr4->As<X>(); // my goal, FAILS
} // As

};
/*
gives:

x.cpp: In member function 'X cNode<D, T>::MyAs(T, X&)':
x.cpp:200: error: expected primary-expression before 'int'
x.cpp:200: error: expected `;' before 'int'
x.cpp:400: error: expected primary-expression before '>' token
x.cpp:400: error: expected primary-expression before ')' token
*/


Using #line is a really good idea, I'll steal that for my own examples!

Anyway,

#line 100
shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
#line 200
shared_ptr< cAttr<D,T> > attr2; attr2->template As<int>(); // works
#line 300
shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
#line 400
shared_ptr< cAttr<D,T> > attr4; attr4->template As<X>(); // works

The problem is that you're using the name As which depends on the
template paramters D and T, and the compiler knows nothing about what As
could be (especially, that it would be a template) for any specific D and T.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 2 '06 #2
Alf P. Steinbach wrote:
Using #line is a really good idea, I'll steal that for my own examples!
Nice :)
#line 100
shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
#line 200
shared_ptr< cAttr<D,T> > attr2; attr2->template As<int>(); // works
#line 300
shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
#line 400
shared_ptr< cAttr<D,T> > attr4; attr4->template As<X>(); // works The problem is that you're using the name As which depends on the
template paramters D and T, and the compiler knows nothing about what As
could be (especially, that it would be a template) for any specific D
and T.


#line 200
shared_ptr< cAttr<D,T> > attr2; attr2->template As<int>();

#line 400
shared_ptr< cAttr<D,T> > attr4; attr4->template As<X>();

is the finall solution then :)

I was quite close, tried to add, in the main class { } cNode scope
using cAttr<D,T>::As<>;
g++ warned me:
note: use 'cAttr<D, T>::template As' to indicate that it is a template
only I put it in wrong place

btw, it seem to compile fine on newer MSVC even without the template

--
Wymiana starych układów... na nowe układy - prawie jak walka z korupcja.
Walka z wychowaniem seksualnym i erotykÄ… - prawie jak walka z patologiÄ….
PiS - prawie jak prawo i sprawiedliwość... Prawie. Prawie robi różnicę.
Myśl. Głosuj rozsądnie. Nie na tanie hasła. // Rafał Maj Raf256
Mar 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John J. Lee | last post by:
I'm trying define a class to act as a Mock "handler" object for testing urllib2.OpenerDirector. OpenerDirector (actually, my copy of it) does dir(handler.__class__) to find out what methods a...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
1
by: graemeharnish | last post by:
Hello. I'm trying to mod an open source app called TinyERP and inherit from a parent object, and in essence change how _column is defined. I found sample code of: class...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
1
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.