|
-
Apr 15th, 2002, 11:50 AM
#1
redefinition?!
I have two template classes and a regular class.
For simplicity: TClass1, TClass2, and Class3(.h and .cpp).
TClass2 inherites TClass1. In TClass2 I have #include "TClass1.h" And then in Class3, I #include "TClass2.h". Then once in my main.cpp I #include both "Class3.h" and "TClass2.h". Then when I compile I get a redefinition error, saying the class in TClass1 has already been redefined.
Error:
h:\cs131\assignment 3\grid.h(26) : error C2953: 'grid' : template class has already been defined
(grid is the class in TClass1)
What can I do? I need to use both TClass2 and Class3 in main. And Class3 needs to use TClass2 data type (constructor and 2 public functions).
Anyone even know what I'm talking about?!
NOMAD
-
Apr 15th, 2002, 01:38 PM
#2
Wrap your classes up with conditional preprocessor blocks:
Code:
#ifndef TCLASS_H
#define TCLASS_H
template<class t>
class TClass
{
};
#include "TClass.cpp"
#endif
Z.
-
Apr 15th, 2002, 06:20 PM
#3
Z you are totally awsome! Thanks!
-
Apr 16th, 2002, 09:56 AM
#4
On a related topic
This question is related although a different question. Can I and should I be using the #ifndef/#define/#endif on regular header files that don't necessarily have a class in them. Example is one of my headers with just straight functions.
NOMAD
-
Apr 16th, 2002, 10:19 AM
#5
Fanatic Member
You probably should. For example, if you need <cstdio> in one of the functions in your header file and then need it again in your main .cpp file, you'll get redefinition errors.
Alcohol & calculus don't mix.
Never drink & derive.
-
Apr 16th, 2002, 01:46 PM
#6
I put preprocessor conditionals around all of my headers, for safety.
Z.
-
Apr 16th, 2002, 02:47 PM
#7
Monday Morning Lunatic
Originally posted by Wynd
You probably should. For example, if you need <cstdio> in one of the functions in your header file and then need it again in your main .cpp file, you'll get redefinition errors.
If not for the fact that all the standard headers are guarded anyway
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|