Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-26-2005, 09:27 AM   #1 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
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!
Aniviel833 is offline   Reply With Quote
Old 06-26-2005, 12:04 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 06-26-2005, 12:31 PM   #3 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
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?
Aniviel833 is offline   Reply With Quote
Old 06-26-2005, 04:14 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Just post the code, then I'll revise it.
__________________
Valmont is offline   Reply With Quote
Old 06-26-2005, 04:46 PM   #5 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
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.
Aniviel833 is offline   Reply With Quote
Old 06-26-2005, 09:57 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
And where is the rest? So far everything is fine.
__________________
Valmont is offline   Reply With Quote
Old 06-27-2005, 06:04 AM   #7 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
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!
Aniviel833 is offline   Reply With Quote
Old 06-27-2005, 07:59 AM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 06-27-2005, 05:37 PM   #9 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
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
Aniviel833 is offline   Reply With Quote
Old 06-27-2005, 10:57 PM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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!
__________________
Valmont is offline   Reply With Quote
Old 07-01-2005, 08:47 AM   #11 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Kewl, Good thing to know. Thankies.

So, how would it look if you wanted to use more than one variable in Animal?
Aniviel833 is offline   Reply With Quote
Old 07-02-2005, 02:43 AM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Well just add it and see how it looks
__________________
Valmont is offline   Reply With Quote
Old 07-02-2005, 04:05 PM   #13 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
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.
Aniviel833 is offline   Reply With Quote
Old 07-02-2005, 05:31 PM   #14 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
do I seperate it with a comma?
yes
But make sure the class knows that it exist aswell.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-03-2005, 12:08 PM   #15 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
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
Aniviel833 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting