|
 |
|
 |
06-26-2005, 09:27 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Redefinition error
Hey everyone...
I'm a bit of a newb here, so please speak slowly  ...
I'm working in C++, using Dev-C++ as a compiler.
Here's a simplified version of my problem:
Class Animal is inherited by both the Cat class and Dog class. When I include both Cat.cpp and Dog.cpp in a file named test, I get the following error:
In file included from Animal.cpp:
from Dog.h,
from Dog.cpp,
from test.cpp:
Animal.h: redefinition of 'class Animal'
Animal.h: previous definition of 'class Animal'
Can I use #ifndef to solve this problem? If so, I'd appreciate an explanation of it and an example of how it would apply to my problem. (I sort of get what it does but don't understand where I would use it in this instance.)
Thanks a bunch!
|
|
|
06-26-2005, 12:04 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Code:
//Animal header
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
};
#endif //ANIMAL_H
Implement these directives in every header you make. Make this method your standard so it becomes a common thing to do.
__________________
|
|
|
06-26-2005, 12:31 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Thanks for your reply!
I tried this and now I get this error:
In file included from Animal.cpp:
from Dog.h,
from Dog.cpp,
from test.cpp:
Animal.h: redefinition of 'Animal::Animal(bool)'
'Animal::Animal(bool = false)'previously defined here.
No Animal::Animal(bool) member function declared in class 'Animal'
Thanks.
Edit: Oops, I didn't see that you had this all in the same file. How do I use this solution when I'm using Animal.h and Animal.cpp as seperate files?
|
|
|
06-26-2005, 04:14 PM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Just post the code, then I'll revise it.
__________________
|
|
|
06-26-2005, 04:46 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Animal.h
Code:
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal{
public:
Animal(bool f = false);
bool fur;//has fur?
bool getFur();
};
#endif
Animal.cpp
Code:
#include "Animal.h"
Animal::Animal(bool f)
{fur = f;}
bool Animal::getFur(){return fur;}
Thanks alot. 
|
|
|
06-26-2005, 09:57 PM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
And where is the rest? So far everything is fine.
__________________
|
|
|
06-27-2005, 06:04 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Cat.h
Code:
#include <Animal.cpp>
using namespace std;
class Cat :public Animal{
public:
bool getStripes();
Cat(bool s = false);
bool stripes;//has stripes?
};
Cat.cpp
Code:
#include "Cat.h"
Cat::Cat(bool s){
stripes = s;
}
bool Cat::getStripes(){
return stripes;
}
Dog.h
Code:
#include <Animal.cpp>
using namespace std;
class Dog :public Animal{
public:
bool getCollar();
Dog(bool c = false);
bool collar;//has collar?
};
Dog.cpp
Code:
#include "Dog.h"
Dog::Dog(bool c){
collar = c;
}
bool Dog::getCollar(){
return collar;
}
test.cpp
Code:
#include <iostream>
#include <Cat.h>
#include <Dog.h>
int main(){
Animal bowser = Animal(true);
cout << bowser.getFur();
Cat meowser = Cat(false);
meowser.fur=true;
cout << meowser.getFur();
cout << meowser.getStripes();
system("PAUSE");
}
Thanks!
|
|
|
06-27-2005, 07:59 AM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
animal.h
Code:
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
Animal(bool f = false);
bool fur;//has fur?
bool getFur();
};
#endif //ANIMAL_H
animal.cpp
Code:
#include "Animal.h"
Animal::Animal(bool f) : fur(f)
{ }
bool Animal::getFur()
{
return fur;
}
dog.h
Code:
#ifndef DOG_H
#define DOG_H
#include "Animal.h"
class Dog :public Animal
{
public:
bool getCollar();
Dog(bool c = false);
bool collar; //has collar?
};
#endif //DOG_H
dog.cpp
Code:
#include "Dog.h"
Dog::Dog(bool c) : collar(c)
{ }
bool Dog::getCollar()
{
return collar;
}
cat.h
Code:
#ifndef CAT_H
#define CAT_H
#include "Animal.h"
class Cat :public Animal
{
public:
bool getStripes();
Cat(bool s = false);
bool stripes; //has stripes?
};
#endif //CAT_H
cat.cpp
Code:
#include "Cat.h"
Cat::Cat(bool s) : stripes(s)
{ }
bool Cat::getStripes()
{
return stripes;
}
main.cpp (test.cpp)
Code:
#include <iostream>
#include "Cat.h"
#include "Dog.h"
int main()
{
Animal bowser(true);
std::cout << bowser.getFur();
Cat meowser(false);
meowser.fur=true;
std::cout << meowser.getFur();
std::cout << meowser.getStripes();
//We don't like system("pause") :)
std::cin.get();
}
Feel free to ask questions if they arise.
__________________
|
|
|
06-27-2005, 05:37 PM
|
#9 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Yay!
It works now! (Partially because a thread on another site reminded me that I had to work in a project file, which I haven't done much before  )
Thanks for all your help explaining this ifndef stuff, I appreciate it.
One question though:
I assume this:
Code:
Animal::Animal(bool f) : fur(f)
{ }
does the same thing as:
Code:
Animal::Animal(bool f)
{fur = f;}
Is this just your style, or is it more efficient/better programming practice?
Thanks again,
-Stacy
|
|
|
06-27-2005, 10:57 PM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
It is more efficient programming style.
Initializing objects (instead of assigning them) prevents possible creation of temporary objects. Temporary objects are simple objects as well, so they may call a run-time costly constructor. By initializing them we prevent them. For const objects one *must* initialize objects the way I do.
Once you learned the way of pointers you should check out my tutorial on the Rule of Three in the tutorial section. There you'll see with graphix the difference between assignment and initialization. The difference may be huge!
__________________
|
|
|
07-01-2005, 08:47 AM
|
#11 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Kewl, Good thing to know. Thankies.
So, how would it look if you wanted to use more than one variable in Animal?
|
|
|
07-02-2005, 02:43 AM
|
#12 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Well just add it and see how it looks 
__________________
|
|
|
07-02-2005, 04:05 PM
|
#13 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
I don't really get how to add another variable...
What does the colon do? To add a second variable, do I seperate it with a comma? A semicolon? Another colon?
 *Confused*
Thanks.
|
|
|
07-02-2005, 05:31 PM
|
#14 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Quote:
|
do I seperate it with a comma?
|
yes
But make sure the class knows that it exist aswell.
|
|
|
07-03-2005, 12:08 PM
|
#15 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Thanks, redhead.
I was getting some weird error on the simple example I was using, which was why I thought I was doing something wrong. But once I put it into my actual (more complex  ) program, everything compiled fine.
I'm using a vector in my class...do I need to do this with vectors? Because...
Code:
MyClass::MyClass(std::vector<MyOtherClass> v, double d) : aDouble(d)
{
std::vector<MyOtherClass> aVector(v);//This is actually initializing the vector, correct?
}
Also, is this how everything would conventionally be set up?
Thanks,
-Stacy
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
What is this error?
|
john_tran |
Standard C, C++ |
4 |
10-20-2004 09:30 PM |
|
C++ compile error
|
Amaranthine |
Standard C, C++ |
5 |
05-05-2004 09:23 PM |
All times are GMT -8. The time now is 05:43 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|