Have a couple errors.

I just have a couple errors. Help appreciated!
[Error] invalid conversion from 'int' to 'Tree*' [-fpermissive]


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <iomanip>
#include <string>
#include <cstddef>
#include<fstream>
using namespace std;

#include"classes.h"
int main() 
{
 int num_of_squirrels;
 int num_of_trees;
 int skip_factor;
 Squirrel squirrel;
 Tree nuts;

 cout << "Please enter the number of squirrels: " ;
 cin >> num_of_squirrels;

 cout << "Please enter the number of trees: " ;
 cin >> num_of_trees;
 
 int round=1;
 if(round % 2 != 0){
	for(int i=1;i<num_of_squirrels+1;++i){
		cout << "Please enter the skip factor " <<"for squirrel # "<<i<<": ";
 		cin >> skip_factor;
 		int *location=new int[num_of_squirrels];
 		squirrel.set_skip_factor(skip_factor);
		location+=skip_factor;
		
	}

 Squirrel *squirrels = new Squirrel [num_of_squirrels];
 Hoard *hoards = new Hoard [num_of_squirrels];

 for(int i=0; i<num_of_squirrels; i++) {
		squirrels[i];
  }

  Tree *trees = new Tree[num_of_trees];
  
  int count =0;

  while(nuts.get_nuts_left()!=0){
	++count;

	
	squirrel.go_to(num_of_trees);
	
	cout<<"Round: "<<count<<endl;
		for(int i = 1; i<num_of_trees+1; i++){
			trees[i];
			cout<<"Tree "<<i<<" has "<<nuts.get_nuts_left()<<" nuts left "<<endl;
		}
		for(int i = 1; i <=num_of_squirrels; i++){
			cout<<"Squirrel "<<i<<" is at tree "<<squirrel.get_location()<<endl;
			cout << endl;
		}
		nuts.remove_nut();
		}
		
	}

system ("pause");
}


classes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Squirrel;//declares each class
class Nut;
class Tree;
class Hoard;
#include<iostream>
class Squirrel
{
	//attributes
	Nut *nut_carried;
	int skip_factor;
	int location;
	
public:
	Squirrel();
	//methods
	void take_nut(Tree *from_tree);
	void store_nut(Hoard hoard);
	bool has_nut();
	void go_to(Tree *tree);
	int get_skip_factor();
	void set_skip_factor(int factor);
	Tree* get_location();
};


squirrel.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
#include <cstddef>
#include "classes.h"


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

void Squirrel::take_nut(Tree *from_tree)
{
	Tree object;
	object.remove_nut();
}

void Squirrel::store_nut(Hoard hoard)
{
	hoard.add_nut(this->nut_carried);
	nut_carried = NULL;
}

bool Squirrel:: has_nut()
{
	if(this->nut_carried != NULL) {
		return true;
	}
	
	if(this->nut_carried = NULL) {
		return false;
	}
} 

void Squirrel::go_to(Tree *tree)
{
	location;
}

int Squirrel::get_skip_factor()
{	
	return skip_factor;
}
	
void Squirrel::set_skip_factor(int factor)
{
	skip_factor =factor; 
}

Tree* Squirrel:: get_location()
{	
	location;
}


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
#include "classes.h"
Tree::Tree()
{
	this->nuts = new Nut[20];
	this->nuts_left = 20;
}

Tree::~Tree()
{
	delete [] nuts;
}

Nut* Tree::remove_nut()
{
	if(nuts_left > 0) {
		Nut *n;
		n = &nuts[nuts_left-1];
		--nuts_left;
		return n;
	}
	if(nuts_left ==0){
		return 0;
	}
	
}



int Tree::get_nuts_left()
{
	return nuts_left;
}


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 <cstddef>
#include<iostream>
using namespace std;

#include "classes.h"

//constructor for hoard
Hoard::Hoard()
{
	this->owner = NULL;
	this->nuts = new Nut[50];
	this->number_of_nuts = 0;
	
}

//Destructor
Hoard::~Hoard()
{
	//delete the nuts array
	delete [] nuts;
}

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

int Hoard::get_number_of_nuts()
{
	return number_of_nuts;
}


Then I have a nut.cpp that is blank.
Okay? At what line is the error, and in which file?
Ok first thing,

You are declaring the classes but you have to define them.

Start off by making a header file (.h file) for each class, i.e. every .cpp file should have a .h file. Then in the header file, everything should be surrounded by something like:

1
2
3
4
5
6
#ifndef nut_hh_
#define nut_hh_

// ... do your header declarations and forward declarations...

#endif 


then keep the implementation files (although they do need to debugged) and include the header file for that class.

When I copied over your code to see if I got your error, I did not get it. I got a problem with your classes.h file.

simply using
 
class Nut;

is not a full definition of a class even if it has a .cpp file. It is a forward declaration. A forward declaration (of lets say class B) tells the compiler in this class (lets call it class A) I am going to use class B in the implementation file (.cpp) of class A but I do not want to include the header file of class B because I do not want to create a large dependency tree (if class C includes class A and class A includes class B in its header then if B is changed, class A will then have to be completely recompiled and then class C will have to completely recompiled - important to understand for large programs. Anyway, a forward declaration is an incomplete definition, you have to actually define the class in its own header file (which you then include in the .cpp file) and in the implementation file you need to include the header for that class which I do not see.

Just saying
 
class Nut;

means nothing. The compiler just lets you then refer to class in that file but expects a definition elsewhere. if you don't have a definition, the compile will fail with a linking error.

Additionally, I am not sure why you have functions that are supposed to return a value and the only line is location; . This does nothing, I'd review that.

Honestly from the looks of it, I would consider making a controller/manager class which means the main() would only have the creation of controller class, creation of your Squirrel, Nut, and Tree classes (which would then be passed to the controller class) and then a Initialize() function in the controller to begin manipulation of the logic to guide you through the program - you could also let the controller class handle the creation of the Squirrel, Nut, and Tree classes. This would make it much easier to manipulate the data within Squirrel, Nut, and Tree classes
Topic archived. No new replies allowed.