Thread: class definition error

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    class definition error

    I am writting a simply inheritance test program. Master class is dad class. class son and daughter is derieved from dad.

    my code :
    dad.h
    Code:
    #include <iostream>
    using namespace std;
    class dad{
    protected:
        string name;
    public:
        dad(string);
    	dad();
    };
    dad.cpp
    Code:
    #include <iostream>
    #include "dad.h"
    using namespace std;
    dad::dad(){
    }
    dad::dad(string r){
        name=r;
    }
    son.h
    Code:
    #include <iostream>
    #include "dad.h"
    using namespace std;
    
    class son:public dad{
    private:
        int age;
    public:
        son(string,int);
    };
    son.h
    Code:
    #include <iostream>
    #include "dad.h"
    using namespace std;
    class son:public dad{
    private:
        int age;
    public:
        son(string,int);
    };
    son.cpp
    Code:
    #include <iostream>
    #include "son.h"
    using namespace std;
    son::son(string r,int x){
        name=r;
        age=x;
    }
    daughter.h
    Code:
    #include <iostream>
    #include "dad.h"
    
    using namespace std;
    
    class daughter:public dad{
    private:
        int age;
    public:
        daughter(string,int);
    };
    daughter.cpp
    Code:
    #include <iostream>
    #include "daughter.h"
    using namespace std;
    daughter::daughter(string r,int x){
        name=r;
        age=x;
    }
    main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    
    #include "son.h"
    #include "daughter.h"
    
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {	
    	system("PAUSE");
        return EXIT_SUCCESS;
    }
    i compile the project in visual c++. i get this error::
    "Error 1 error C2011: 'dad' : 'class' type redefinition"

    how to over come it? looks like a small error,but i aint able to figure it out.pls help.thanks,.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If the contents of a source file is #include'd more than once (directly or indirectly) the compiler seems multiple definitions of any classes. That's a violation of the one-definition rule.

    Common practice is to employ include guards on all headers of the form;
    Code:
    #ifndef SOME_MACRO_UNIQUE_TO_THIS_HEADER_FILE
    #define SOME_MACRO_UNIQUE_TO_THIS_HEADER_FILE
    
    //   the content you have in the header file
    
    #endif
    You need to employ some consistent approach to ensure no two header files use the same macro for their include guard.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your header don't have include guards. So, if you include both "daughter.h" and "son.h", the declaration of the dad class gets included twice.

    Code:
    #ifndef SOME_IDENTIFIER_EG_HEADERS_FILENAME_H
    #define SOME_IDENTIFIER_EG_HEADERS_FILENAME_H
    
    class X
    {
    ...
    };
    
    #endif
    You may also be misunderstanding what inheritance is in programming. Does it make sense to say that "son is a kind of dad" and "daughter is a kind of dad"?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by anon View Post
    You may also be misunderstanding what inheritance is in programming. Does it make sense to say that "son is a kind of dad" and "daughter is a kind of dad"?
    However, legally, sons and daughters expect to inherit from their parents. And they can also be said to inherit characteristics.

    But, yeah, these meanings of inheritence differ from the meaning employed in object oriented design, in which "inherits" means "is a kind of".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Another problem is that you are including the wrong header file.
    std::string uses the <string> header file, not the <iostream> header file.
    It's also bad practice to put "using namespace" lines in a header file.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM