Thread: redefine error (multiple files)

  1. #1
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13

    Angry redefine error (multiple files) [SOLVED]

    ah never mind I'm getting ahead of myself. I never implemented the destructor. Sorry for the waist of a post. I guess there should be a rule like think about a problem for at least 24 hours before posting

    When I compile I get this error, even though I assign the variables a value only once in a single file.

    >C:\MinGW\bin\g++ -pedantic -Os main.cpp -o main.exe
    In file included from Tree.cpp:2,
    from Tree.h:18,
    from main.cpp:2:
    Branch.h:2: error: redefinition of `const int LOWEST'
    Branch.h:2: error: `const int LOWEST' previously defined here
    Branch.h:3: error: redefinition of `const int HEIGHEST'
    Branch.h:3: error: `const int HEIGHEST' previously defined here
    Branch.h:7: error: redefinition of `class Branch'
    Branch.h:7: error: previous definition of `class Branch'


    main.cpp
    Code:
    #include <iostream>
    #include "Tree.h"
    
    using namespace std;
    
    int main()
    {
        Tree myTree = Tree();    
        return 0;
    }
    branch.h
    Code:
    const int LOWEST = 1;
    const int HEIGHEST = 2;
    //I am uneasy about these being in the global scope of the interface file
    
    class Branch
    {
        public:
            Branch();
            ~Branch();
            
        private:
            int currentTime, dielemas;
            int randNumber();
    };
    #ifndef Branch_INCLUDED
    #define Branch_INCLUDED
    #include "Branch.cpp"
    #endif
    Branch.cpp
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    Branch::Branch()
    {
        int *onBranch = NULL;
        dielemas = 0;
        currentTime = 0;
     
        srand((unsigned)time(0));
        while(Branch::randNumber() == 1)
        {
            onBranch = (int *) realloc(onBranch, (dielemas+1)*sizeof(int));
            onBranch[dielemas++] = currentTime;
        }
    }
    
    int Branch::randNumber()
    {
        int random_integer;
        int range=(LOWEST-HEIGHEST)+1;
        return random_integer = LOWEST+int(range*rand()/(RAND_MAX + 1.0));
    }
    Tree.h
    Code:
    #ifndef BRANCH_INCLUDED
    #define BRANCH_INCLUDED
    #include "Branch.h"
    #endif
    
    const int TREE_HEIGHET = 20;
    
    class Tree
    {
        public:
            Tree();
            Branch getBranch(int level);
        private:
            Branch treeBranch[TREE_HEIGHET];
    };
    #ifndef TREE_INCLUDED
    #define TREE_INCLUDED
    #include "Tree.cpp"
    #endif
    Tree.cpp
    Code:
    #include <iostream>
    #ifndef BRANCH_INCLUDED
    #define BRANCH_INCLUDED
    #include "Branch.h"
    #endif
    
    using namespace std;
    
    Tree::Tree()
    {
        Branch dielema;//dielema is the variable with the caged birds
        for(int i = 0; i<TREE_HEIGHET; i++)
        {
            
            dielema = Branch();
            treeBranch[i] = dielema;
        }
        
        
    }
    The lines in bold I added later. Now whenever I try to compile I get >C:\MinGW\bin\g++ -pedantic -Os main.cpp -o main.exe
    C:\DOCUME~1\US\LOCALS~1\Temp/cc0GWiF7.o:main.cpp.text+0x152): undefined reference to `Branch::~Branch()'
    C:\DOCUME~1\US\LOCALS~1\Temp/cc0GWiF7.o:main.cpp.text+0x190): undefined reference to `Branch::~Branch()'
    C:\DOCUME~1\US\LOCALS~1\Temp/cc0GWiF7.o:main.cpp.text+0x1a3): undefined reference to `Branch::~Branch()'
    C:\DOCUME~1\US\LOCALS~1\Temp/cc0GWiF7.o:main.cpp.text+0x1d8): undefined reference to `Branch::~Branch()'
    C:\DOCUME~1\US\LOCALS~1\Temp/cc0GWiF7.o:main.cpp.text+0x290): undefined reference to `Branch::~Branch()'
    C:\DOCUME~1\US\LOCALS~1\Temp/cc0GWiF7.o:main.cpp.text+0x2ce): more undefined references to `Branch::~Branch()' follow
    collect2: ld returned 1 exit status
    Last edited by help_seed; 11-30-2009 at 12:56 AM. Reason: removed comments [SOLVED]

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just as an FYI, I'd rethink how you set up your header and source files. You aren't following standard practice with your header include guards, and you also should not normally #include cpp files. I'd suggest doing some more research on header include guards and setting up projects with multiple files. If you have questions feel free to post them here (make sure to mention which IDE or compiler you use).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  2. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM