Need serious help with classes!

I NEED TO KNOW HOW TO ACCESS EACH INDIVIDUAL SQUIRRELS ATTRIBUTES SUCH AS SKIP FACTOR AND LOCATION ON EACH ROUND!

Ok I think the header files and cpp files are correct. The program is compiling as-is right.

Odd round: squirrel goes to tree and gets a nut unless there is a another squirrel, then no nut.
Even round: squirrel goes to hoard and deposits nuts.

I need to be able to display----
Round 1:
Squirrel 1 went to tree 3 and got 1 nut
squirrel 2 went to .......
squirrel 3 went to. ... . ..
Tree 1 has 19 nuts left
Tree 2 has .....

Round 2:
Squirrel 1 has deposited
squirrel 2 didnt deposit due to fighting
.....

This is why I need to be able to access the values inside of each Squirrel

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
#include <iostream>
#include <iomanip>
#include <cstddef>
#include"squirrel.h"
#include"tree.h"
#include"hoard.h"
#include"nut.h"
using namespace std;

int main(){
	int num_of_squirrels;
	int num_of_trees;
	int skip_factor;
	Squirrel squirrel;
	Tree nut;
	
	cout << "Please enter the number of squirrels:  ";
	cin >> num_of_squirrels;
	cout << "Please enter the number of trees:  ";
	cin >> num_of_trees;
	//Creating the correct amount
	Squirrel *squirrels = new Squirrel[num_of_squirrels];
	Hoard *hoards = new Hoard [num_of_squirrels];
	Tree *trees = new Tree[num_of_trees];
	//Getting and setting skip factor
	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 - i];
 	squirrel.set_skip_factor(skip_factor);
}	
}


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
#include<iostream>
using namespace std;
#include"squirrel.h"
#include"tree.h"
#include"nut.h"
#include"hoard.h"


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

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(nut_carried!=NULL){
		return true;
	}
	else{
		return false;
	}
}

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

int Squirrel::get_skip_factor()
{
	return skip_factor;
}

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


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


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
43

#include <cstddef>
#include<iostream>


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


nut.cpp
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include"squirrel.h"
#include"tree.h"
#include"nut.h"
#include"hoard.h" 

Nut::Nut()
{
	
}


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
26
#pragma once
#include <iostream>
#include "hoard.h"
#include "nut.h"
#include "tree.h"

class Hoard;
class Tree; 
class Squirrel;

class Squirrel
{
	Nut* nut_carried;
	int skip_factor;
	
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* location;

};


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


class Tree;
class Nut;
class Squirrel;
class Hoard;

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


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

class Hoard;
class Squirrel;
class Tree;
class Nut;

class Hoard{

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


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



class Nut;

class Nut
{
public:
	Nut();
};
Last edited on
mind if i download a copy of this to find out what the error is?
By all means. It is all right there so you can copy and paste I guess. I really need help accessing and setting the attributes of the squirrel. It compiles as is though.
Last edited on
treyrox wrote:
Ok I think the header files and cpp files are correct.
main(){???
Better to explicitly type int as some compilers will flag this as error, even though int may be a default return type.

In line 30(main.cpp) change to: squirrel[i-1].set_skip_factor(skip_factor); as your previous code deals only with squirrel[0].
Also, in C++, arrays are zero-based, so I think you would want to revisit the for loop in main.cpp(also for other problems).
What's with the pointer location? for your info, this wastes memory as it is created and uses new memory each time the loop iterates. Consider using the keyword static.

Aceix.
Last edited on
@Aceix

I removed the location to deal with at another time and replaced my code with yours and it gave me this error:

29 15 M:\Group Project 2\main.cpp [Error] no match for 'operator[]' in 'squirrel[(i + -1)]'
I meant (*squirrels)[i-1].set_skip_factor(skip_factor); dealing with the pointer-to-array.

Aceix.
@Aceix "(*squirrels)[i-1].set_skip_factor(skip_factor);" you need to remove the * from inside of the parenthesis's
@lolnyancats yeah, saw that. Thanks.
squirrels[i-1].set_skip_factor(skip_factor);

Aceix.
Last edited on
yeah i already saw and fixed it. I also repaired the .set_skip_factor ..... With you answer you pretty much answered all my questions!!!

I love you
Topic archived. No new replies allowed.