Archived

This topic is now archived and is closed to further replies.

Punx

C++ Class Problem

Recommended Posts

Punx    122
One of my classes (for this example Class1) uses an object of another class (Class2) as a parameter for a method. When I try to compile I get: error C2061: syntax error : identifier ''Class2'' So I thought to fix this problem I would add... #include "class2.h" to ''Class1.h''. But now whenever I compile I get an error saying: error C2011: ''Class2'' : ''class'' type redefinition I know I''m doing something wrong but I don''t know how to correct it.

Share this post


Link to post
Share on other sites
cyn    138
you need to put #ifndef into your code


  
//class1.h

#ifndef CLASS1
#define CLASS1

// your code here


#endif


this tells the compiler that if it has already included this section of code, not to do it again (redefinition)

cyn

EDIT - fixed source tags

[edited by - cyn on May 5, 2002 11:02:23 PM]

Share this post


Link to post
Share on other sites
Fruny    1658
It''s a FAQ.

class1.h

#ifndef CLASS1_HEADER
#define CLASS1_HEADER

class class2; // declare class2

class class1 // define class1
{
// can now use class2 (only) as return type,
// parameter, pointer or reference.
}

#endif


class2.h

#ifndef CLASS2_HEADER
#define CLASS2_HEADER


class class1; // declare class1

class class2 // define class2
{
// can now use class1 (only) as return type,
// parameter, pointer or reference.
}
#endif


Your source (.cc, .C, .cpp, .cxx ...) files will still need to include both class1.h and class2.h

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
Fruny    1658
cyn: he''s got a recursive definition problem, not just a multiple header inclusion problem.

There is an article by Kylotan there.
Go study it and you will be enlightened

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites