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