Program not compiling.

I don't understand why this program will not compile. I know the program is kind of long but I would really appreciate any help. I am not very advanced in C++ so I figured some of you guys would be able to pick out the problem. Thanks!

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
//#include<cstdef>
#include<iomanip>
#include<fstream>
#include"squirrels.h"
#include"tree.h"
#include"nuts.h"
#include"hoard.h"
using namespace std;

int main()
{
	int numSq[];
	int numTree[];
	int skipFactor;
	Tree nuts;
	Squirrel squirrel;
	
	cout<<"Please enter the number of squirrels: "<<endl;
	cin>>numSq;
	for(int i=0;i<=numSq;++i){
		numSq[i];
	}
	
	cout<<"Please enter the number of trees: "<<endl;
	cin>>numTree;
	for(int i=0;i<=numTree;++i){
		numTree[i];
	}
	
	cout<<"Please enter the skip factor of squirrel: "<<i<<endl;
	cin>>skipFactor;
	set_skip_factor(skipfactor);
	
	
	int count=0;
	while(nuts.nuts_left!=0){
		++count;
		cout<<"Round: "<<count<<endl;
	for(int i=0;i<=numTree;++i){
		cout<<"Tree "<<numTree[i]<<" has "<<nuts.nuts_left<<" nuts left"<<endl;
	}
	for(int i=0;i<=numSq;++i){
		cout<<"Squirrel "<<numSq[i]<<" is at Tree "<<squirrel.location;
		squirrel.location+=skipFactor;
	}
}


squirrels.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;
#include"nuts.h"
#include"tree.h"
#include"hoard.h"


class Squirrel
{
	Nut* nut_carried;
	int skip_factor;
	Tree* location;
public:
	Squirrel();
	void take_nut(Tree* from_tree);
	void store_nut(Hoard hoard);
	bool has_nut();
	void go_to(Tree *tree);
	void get_skip_factor();
	void set_skip_factor(int factor);
};


squirrels.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
using namespace std;
#include"squirrels.h"
#include"tree.h"
#include"nuts.h"
#include"hoard.h"


Squirrel::Squirrel()
{
	this->nut_carried=NULL;
	this->skip_factor=1;
	this->location=NULL;
}

Squirrel::~Squirrel()
{
	delete[]Squirrel;
}

void take_nut(Tree* from_tree)
{
	remove_nut();
}

void store_nut(Hoard hoard)
{
	add_nut();
	nut_carried=NULL;
	
}

bool has_nut()
{
	if(nut_carried!=NULL){
		return true;
	}
	else{
		return false;
	}
}

void go_to(Tree *tree)
{
	location=tree;
	return NULL;
}

int get_skip_factor()
{
	return skip_factor;
}

void set_skip_factor(int factor)
{
	for(int i=0;i>0;++i){
		skip_factor=[i];
	}
}


tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

class Tree
{
	Nut* nuts;
	int nuts_left;
	
public:
	Tree();
	~Tree();
	Nut* removeNut();
	int get_nuts_left();
		
};


tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;
#include"squirrels.h"
#include"tree.h"
#include"nuts.h"
#include"hoard.h"
using namespace std;


Tree::Tree()
{
	this->nuts_left=20;
	this->nuts=new nut[20];
}
Tree::~Tree()
{
	delete [] nuts;
}
Nut* removeNut()
{
	if(nuts_left>0){
		nuts[nuts_left-1];
		return nuts_left;
		--nuts_left;
	}
	else if(this->nuts_left==0){
		return NULL;
	}
}

int get_nuts_left()
{
	return nuts_left;
}


nuts.h
1
2
3
4
5
6
#include<iostream>
using namespace std;

class Nut
{
};


nuts.cpp
1
2
3
4
5
6
#include<iostream>
using namespace std;
#include"squirrels.h"
#include"tree.h"
#include"nuts.h"
#include"hoard.h" 


hoard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
#include"squirrels.h"


class Hoard
{
	Squirrel* owner;
	Nut* nuts;
	int numberOfNuts;
public:
	Hoard();
	~Hoard();
	int add_nut(Nut *nut);
	int get_number_of_nuts();
	
};


hoard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
using namespace std;
//#include"squirrels.h"
//#include"tree.h"
//#include"nuts.h"
#include"hoard.h"


Hoard::Hoard()
{
	this->owner=NULL;
	this->nuts=new nut[50];
	this->numberOfNuts=0;
}
Hoard::~Hoard()
{
	delete[] nuts;
}

int add_nut(Nut *nut)
{
	if(numberOfNuts<50){
		nuts[numberOfNuts]=*nut
		++numberOfNuts;
		return 0;
	}
	else{
		return 1;
	}
}

int get_number_of_nuts()
{
	int count=0;
	while(nuts[count]!=NULL){
	++count;
}
	return count;
}
What is the first error returned by the compiler?

Also, what is this?

1
2
3
4
5
6
7
int numSq[];
	...
	cout<<"Please enter the number of squirrels: "<<endl;
	cin>>numSq;
	for(int i=0;i<=numSq;++i){
		numSq[i];
	}


numSq is a pointer to an int, and then you write directly into that, changing its value to some location (which you also intend to be the number of squirrels, which is absolutely insane), then you loop based on the value of that pointer and all you do in the loop is nothing?

I think you don't understand what a pointer is, and that's going to cripple you. Please read about pointers: http://www.cplusplus.com/articles/EN3hAqkS/
Last edited on
and second, third, so on and so fo(u)rth.
The program crashes. Doesn't give any errors unless you press cancel while it is compiling. Compiler doesn't finish itself. Its like it gets caught in a loop.
Read my earlier edit. You've gone horribly wrong in understanding pointers.
That was suppose to put each squirrel into an array. Like squirrel 1, squirrel 2..so on.
1
2
3
4
5
6
7
8
int numSq; // This is just an int.
cout<<"Please enter the number of squirrels: "<<endl;
cin>>numSq;

// Now need to make an array big enough for all the squirrels
int* squirrelArray = new Squirrel[numSq];

// Done. squirrelArray is the array of squirrels  

Thanks, Moschops. Still won't compile. When I press cancel it takes me directly to hoard.h and has an error by #include"squirrels.h"
http://en.wikipedia.org/wiki/Include_guard

This will go a lot faster if you read the actual error as well as just looking where the magic arrow points.
Last edited on
Topic archived. No new replies allowed.