Thread: Splitting up class definitions

  1. #1
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23

    Splitting up class definitions

    Hello All!

    I am attempting to split up a class into two files, a .h and a .cpp but I'm getting so many errors that it's mad. Basically I have in my header file:

    Code:
    #if !defined MY_CLASSES_H
    #define MY_CLASSES_H
    
    #include <string>
    #include "date.h"
    using namespace std;
    
    class this_class
    {
    
    public:
    this_class();
    void function1();
    int function2(int var1, float var2);
    
    private:
    int var1;
    int var2;
    date var3;
    string var4;
    
    };
    
    class that_class : public this_class
    {
    
    public:
    that_class();
    date function3();
    
    private:
    int var5;
    string var6;
    
    };
    and the .cpp has...

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <time.h>
    #include "date.h"
    #include "functions.h"
    #include "Bank_Acc.h"
    using namespace std;
    
    this_class::this_class()
    {
    
    //Do Stuff
    
    }
    
    void this_class::function1()
    {
    
    //More Stuff
    
    }
    ........
    date that_class::function3()
    {
    
    //Yet More Stuff
    
    }
    My compiler keeps telling me that I have one or more multiply defined symbols. Is it even possible to declare 2 classes in a header file and define their functions in a .cpp file? If do you have to define them each in their own header files with defined functions in seperate .cpp files and if so how do I make one class inherit the details of another class using this method (including the calling of the constructor for the parent class)?

    Cheers for your help guys and gals!
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Generally:

    MyClass.h:
    Code:
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include "whatever_files_you_need.h"
    
    class MyClass
    {
      public:
        void MyMethod();
    
      protected:
        int MyMemberVariable;
    };
    
    #endif
    MyClass.cpp:
    Code:
    #include "MyClass.h"
    
    void MyClass::MyMethod()
    {
      MyMemberVariable = 1337;
    }
    You can have multiple classes in the same file, however it usually gets confusing for the readers so it's not adviced. During small (test?) programs it might be ok though.

    If you get an error of multiple defined symbols you usually forgot the inclusion guards. I see you used it thogh, perhaps the error lies in one of the other files you included?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    This one seems to have me well and truly whipped. It keeps complaining that I'm redefining the functions but I can't figure out why. If you can shed anymore light on this it would be appreciated. Cheers
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Are you using this....
    Code:
    #if !defined MY_CLASSES_H
    #define MY_CLASSES_H
    (which btw is missing an #endif )

    Or are you using
    Code:
    #ifndef MY_CLASSES_H
    #define MY_CLASSES_H
    .
    .
    #endif
    I also noticed both those files include "date.h", is that header defined as well?
    Last edited by Scribbler; 02-21-2005 at 12:13 PM.

  5. #5
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    Sorry I have the endif in the actual file, I would copy and paste them direclty but there's about 1000 lines of code which is why I really want to split them. For some reason I'm getting the same errors even if I try to define the functions after the class declaration. Maybe I'm not declaring them correctly or something :S
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM