Having trouble will classes and objects

Looking for guidance in this time of need. 20 nuts in a tree 50 per hoard 2 squirrels in one tree then no nuts because they fight. Must print out each round the status of trees and squirrels.

I think that the logic is pretty sound, just need to get rid of these last erros.

squirrel.h
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
#pragma once
#include <iostream>

#include "nut.h"
#include "tree.h"
#include "hoard.h"
#include "squirrel.h"
class Squirrel;

class Squirrel
{
	Nut *nut_carried;
	int skip_factor;
	int location;
	
public:
	Squirrel();
	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();
};


hoard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <iostream>

#include "nut.h"
#include "squirrel.h"

class Hoard;

class Hoard{
	Squirrel *owner;
	Nut *nuts;
	int number_of_nuts;

public:
	int add_nut(Nut *nut);
	int get_number_of_nuts();
};


nut.h
1
2
3
4
5
6
7
#pragma once
#include <iostream>

class Nut;

class Nut{
};


tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once
#include <iostream>


class Tree;

class Tree{
	Nut *nuts;
	int nuts_left;
	
public:
	Nut* remove_nut();
	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
35
36
#include"squirrel.h"
#include"tree.h"
#include"hoard.h"
#include"nut.h"
#include <iostream>
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;
}

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
54
55
56
57
#include <cstddef>
#include"squirrel.h"
#include"tree.h"
#include"hoard.h"
#include"nut.h"
#inlude <iostream>


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;
}


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

#include"squirrel.h"
#include"tree.h"
#include"hoard.h"
#include"nut.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;
}


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
67
68
#include <iostream>
#include <iomanip>
#include <string>
#include <cstddef>
#include<fstream>
using namespace std;
#include"squirrel.h"
#include"tree.h"
#include"hoard.h"
#include"nut.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: "<< endl;
 cin >> num_of_squirrels;

 cout << "Please enter the number of trees: " << endl; ;
 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<<": "<< endl;
 		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");
}


I am getting the following errors:
Line 10 col 2: Squirrel does not name a type
Line 47 col 28: nuts.get_nuts_left cannot be used as a function
line 51 col 29: invalid conversion from 'int' to 'Tree'
line 21 col 7: initializing argument 1 of void Squirrel::go_to(Tree*)
line 56 col 50: nuts.Tree::get_nuts_left cannot be used a s function
line 67 cold 16: system not declared

Last edited on
Hi,

Don't have using namespace std; there is a C++11 std::round, and a std::count

Look at my previous posts as to why & how.

When posting errors, post them in full and make sure the line numbers match the files you post here. And the name of the file.

Your variables name a little confusing: line 17 Tree nuts; especially when there is a class called nut !!

Why do your for loops start at 1 and add 1 to the end condition and have <= operator ?

Why do you use the this pointer? It is handy for overloading operators, but no need in classes.

Squirrel::go_to Take a point to tree, not an int.

Line 47 col 28: nuts.get_nuts_left cannot be used as a function


Tree::get_nuts_left is a member variable, not a function.

system is a bad idea any way, just std::cin a variable if you need to hold the window open.

Hope this helps :+)

Topic archived. No new replies allowed.