class definition recursion?

S

SpreadTooThin

I am having trouble getting a piece of code to compile on VC++ 6.0...
Compiles fine on MAC OS X under X-Code....


#include <list>
class myClass
{
private:
std::string name;
std::list<myClass> objects;

};


I tried putting class myClass; in front of the class definition but it
didn't help...

errors...
C:\program files\microsoft visual studio\vc98\include\list(29) : error
C2079: '_Value' uses undefined class 'myClass'
C:\Documents and Settings\username\Desktop\deleteme
\deleteme.cpp(15) : see reference to class template instantiation
'std::list<class myClass,class std::allocator<class myClass> >' being
compiled
c:\program files\microsoft visual studio\vc98\include
\functional(185) : error C2079: 'value' uses undefined class 'myClass'
c:\program files\microsoft visual studio\vc98\include
\list(285) : see reference to class template instantiation
'std::binder2nd<struct std::not_equal_to<class myClass> >' being
compiled
C:\Documents and Settings\user\Desktop\deleteme
\deleteme.cpp(15) : see reference to class template instantiation
'std::list<class myClass,class std::allocator<class myClass> >' being
compiled
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I am having trouble getting a piece of code to compile on VC++ 6.0...
Compiles fine on MAC OS X under X-Code....


#include <list>
class myClass
{
private:
std::string name;
std::list<myClass> objects;

};


I tried putting class myClass; in front of the class definition but it
didn't help...

Except for having forgotten to include <string> it looks fine to me. Any
special reason for using VC++ 6, if not go and get VC++2005 Express and
it will work.
 
N

Naresh Rautela

I am having trouble getting a piece of code to compile on VC++ 6.0...
Compiles fine on MAC OS X under X-Code....

#include <list>
class myClass
{
private:
std::string name;
std::list<myClass> objects;

};

I tried putting class myClass; in front of the class definition but it
didn't help...

errors...
C:\program files\microsoft visual studio\vc98\include\list(29) : error
C2079: '_Value' uses undefined class 'myClass'
C:\Documents and Settings\username\Desktop\deleteme
\deleteme.cpp(15) : see reference to class template instantiation
'std::list<class myClass,class std::allocator<class myClass> >' being
compiled
c:\program files\microsoft visual studio\vc98\include
\functional(185) : error C2079: 'value' uses undefined class 'myClass'
c:\program files\microsoft visual studio\vc98\include
\list(285) : see reference to class template instantiation
'std::binder2nd<struct std::not_equal_to<class myClass> >' being
compiled
C:\Documents and Settings\user\Desktop\deleteme
\deleteme.cpp(15) : see reference to class template instantiation
'std::list<class myClass,class std::allocator<class myClass> >' being
compiled

Works for me. I am using Visual C++ 2005 Express Edition though.
 
S

SpreadTooThin

Except for having forgotten to include <string> it looks fine to me. Any
special reason for using VC++ 6, if not go and get VC++2005 Express and
it will work.

Yes your right i forgot to include <string> in the posting here.. but
not in the code.
Well I'm trying to write cross platform code.. for windows and mac os
x...
All I have for development on windozs is Visual C++ 6.0

Just curious but the application I tried to make was a win32 console
application...
 
P

Pete Becker

SpreadTooThin said:
#include <list>
class myClass
{
private:
std::string name;
std::list<myClass> objects;

};

At the point where myClass::eek:bjects is declared, the type myClass is
incomplete (it's not complete until the closing curly brace). The
behavior of a program that uses an incomplete type as a template
argument is undefined (in most cases, including this one). So an
implementation is not required to accept this code or to do anything
sensible with it.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
G

Gavin Deane

Yes your right i forgot to include <string> in the posting here.. but
not in the code.

That sort of mistake should never happen, and ensuring it doesn't is
trivial. If you are going to post code in a message, you should copy
and paste *directly* from your code editor into your message, as per
FAQ 5.8. Whatever you did instead - maybe you copied and pasted
separate snippets (bad), maybe you typed code directly into your post
(very bad) - you managed to leave a line out. In this case it was
obvious what you had omitted and it didn't affect things. Next time,
the bit you accidentally fail to post could be absolutely crucial to
understanding your problem and providing the answer. If that happens,
you might find you get little or no help, or, worse, you and the
people trying to help you could end up on a wild goose chase because
of the misunderstanding.
Well I'm trying to write cross platform code.. for windows and mac os
x...
All I have for development on windozs is Visual C++ 6.0

That compiler is archaic. As Erik Wikström said, get Visual C++ 2005
Express. It's free to download so there's nothing stopping you getting
up to date.

Gavin Deane
 
G

Gavin Deane

At the point where myClass::eek:bjects is declared, the type myClass is
incomplete (it's not complete until the closing curly brace). The
behavior of a program that uses an incomplete type as a template
argument is undefined (in most cases, including this one). So an
implementation is not required to accept this code or to do anything
sensible with it.

I thought there was a rule about not using incomplete types as
template arguments, but Comeau online compiled the OP's code with no
problem. That's not inconsistent with undefined behaviour, but the
only thing I can find in the standard (1998 version) is a note in
14.3.1/2 that says "a template type argument may be an incomplete
type". Have I missed something?

Gavin Deane
 
N

Noah Roberts

Gavin said:
I thought there was a rule about not using incomplete types as
template arguments, but Comeau online compiled the OP's code with no
problem. That's not inconsistent with undefined behaviour, but the
only thing I can find in the standard (1998 version) is a note in
14.3.1/2 that says "a template type argument may be an incomplete
type". Have I missed something?

Gavin Deane

I think the problem here is that the OP is creating an object of the
template instance type as part of the type in the template's argument.
This instance cannot be constructed...or at least the standard doesn't
say what happens apparently.

If the OP were to use a pointer here I /think/ they would be ok. They
would need to create the object in an area of code where "myClass" was
fully defined.
 
P

Pete Becker

Gavin said:
I thought there was a rule about not using incomplete types as
template arguments, but Comeau online compiled the OP's code with no
problem. That's not inconsistent with undefined behaviour, but the
only thing I can find in the standard (1998 version) is a note in
14.3.1/2 that says "a template type argument may be an incomplete
type". Have I missed something?

No, I missed a bit of precision: what I said about myClass above applies
to standard library components, not to templates in general.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
P

Pete Becker

Pete said:
No, I missed a bit of precision: what I said about myClass above applies
to standard library components, not to templates in general.

Sorry, hit Send too soon. The behavior is undefined, which means that
there are no constraints imposed by the standard on what the
implementation does. Once thing it can do is compile the code in the way
you'd expect. But even if it does compile in this simple form, I'm
betting it won't if you add inline functions, defined in the class body,
that do things to myClass::eek:bjects that require knowing something about
myClass.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
G

Gavin Deane

Sorry, hit Send too soon. The behavior is undefined, which means that
there are no constraints imposed by the standard on what the
implementation does. Once thing it can do is compile the code in the way
you'd expect. But even if it does compile in this simple form, I'm
betting it won't if you add inline functions, defined in the class body,
that do things to myClass::eek:bjects that require knowing something about
myClass.

Thanks. In practice I couldn't work out what code like you suggest
could logically do so undefined behaviour isn't a surprise, but I'd
been trying to find the rule in the templates section of the standard.
Now you've told me where to look, I've found it. If anyone wants the
chapter and verse, it's 17.4.3.6/2.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top