HOMEWORK HELP - Just so i stick by the rules.

The second part of my assignment is to create an array object, and love and behold, im having problems passing my small array into the function.

Bellow is my code of what i have done so far, the solution seems to scream pointers in my head, but classes is new to me and i just cant see how it would work?!

So Code bellow:

Header File

//Create objects called asteroid, and apply conditions to them. Once the asteroids have values tied to them,
//then need to check to see if the energy matches and if the asteroids have collided. 
#pragma once
class coord
{
private:
	int xx;
	int yy;
	int velocity;
	int energy;
	//Methods
public:
	//Constructors
	coord(int xxIn, int yyIn, int velocityIn, int energyIn);
	coord(void);
	//Getters
	int getxx(void);
	int getyy(void);
	int getvelocity(void);
	int getenergy(void);
	//Setters
	void updatex(int xxIn);
	void updatey(int yyIn);
	void setvelocity(int velocityIn);
	void setenergy(int energyIn);
	void collision(coord[]);//pass the class in. set to array[] in attempt to fix code
	void energycomp(coord[]);//pass the class in. set to array[] in attempt to fix code
	void printing(coord[]);//Printing values. set to array[] in attempt to fix code
	//hoping that by changing the prototypes to match the rest of the code, passing arrays will work
};

Implimentation CPP

#include "coord.h"
#include<iostream>
using namespace std;
coord::coord(void)
:xx(0)
,yy (0)
,velocity(0) 
,energy(0)
{
}
coord::coord(int xxIn, int yyIn, int velocityIn, int energyIn)
{
	xx = xxIn;
	yy = yyIn;
	velocity = velocityIn;
	energy = energyIn;
}
int coord::getxx(void)
{
	//To get x
	return xx;
}
int coord::getyy(void)
{
	//To get y
	return yy;
}
int coord::getvelocity(void)
{
	//To get velocity
	return velocity;
}
int coord::getenergy(void)
{
	//To get energy
	return energy;
}
//Setters
void coord::updatex(int xxIn)
{
	//To store in x value
	xx = xx + xxIn;
}
void coord::updatey(int yyIn)
{
	//To store in y value
	yy = yy + yyIn;
}
void coord::setvelocity(int velocityIn)
{
	//To store velocity
	velocity = velocity + velocityIn;
}
void coord::setenergy(int energyIn)
{
	//To store energy
	energy = energy + energyIn;
}
void coord::collision(coord asteroid[])
{
	//To check if the objects have collided
	for(int i = 0; i<2; i++)
	{
		if(this->xx == asteroid[i].getxx() && this->yy == asteroid[i+1].getyy())
		{
			cout<<"Collision"<<endl;
		}
	}
}
void coord::energycomp(coord asteroid[])
{
	//To check if the objects energies match
	for(int i = 0; i<2; i++)
	{
		if(asteroid[i].getenergy() == asteroid[i+1].getenergy())
		{
			cout<<"Energy Signatures match"<<endl;
		}
	}
}
void coord::printing(coord asteroid[])
{
	//To print values
	for(int i = 0;i < 2; i++)
	{
		cout<<"Asteroid"<<asteroid[i]<<"Details: "<<endl;
		cout<<asteroid[i].getxx()<<","<<asteroid[i].getyy()<<endl;
		cout<<"Energy: "<<asteroid[i].getenergy()<<endl;
		cout<<"Velocity: "<<asteroid[i].getvelocity()<<endl;
		cout<<endl;
	}
}

Main

#include "coord.h"
#include<iostream>
using namespace std;

int main()
{
	//Variables
	int astx = 0;
	int asty = 0;
	int asten = 0;
	int astvel = 0;
	////////////
	//Creating objects
	coord asteroid[2];
	for(int i = 0; i<2; i++)
	{
		astx = 0;
		asty = 0;
		asten = 0;
		astvel = 0;
		//////
		cout<<"Set asteroid "<<i<<" X"<<endl;
		cin>>astx;
		asteroid[i].updatex(astx);
		cout<<"Set asteroid "<<i<<" Y"<<endl;
		cin>>asty;
		asteroid[i].updatey(asty);
		cout<<"Set asteroid "<<i<<" Energy"<<endl;
		cin>>asten;
		asteroid[i].setenergy(asten);
		cout<<"Set asteroid "<<i<<" Velocity"<<endl;
		cin>>astvel;
		asteroid[i].setvelocity(astvel);
	}
	//Collision check
	for(int i = 0; i< 2; i++)
	{
		asteroid[i].collision(asteroid[i+1]);
	}
	//Energy check
	for(int i = 0; i< 2; i++)
	{
		asteroid[i].energycomp(asteroid[i+1]);
	}
	//Printing to screen
	for(int i = 0; i< 2; i++)
	{
		asteroid[i].printing(asteroid[i+1]);
	}
	//
	system("pause");
}

My layout may seem strange, my lecturer insists that everything be kept in this style, class definition in header, all functions in a seperate .cpp and minimal main.

Thanks for any help offered

Very nice. Now if we can only get what the problem is, then it would be nice of you.

why exactly are you passing an array to your collision method? wouldn't you just want to pass one object or are you trying to see if everything in that array will collide with the calling object?

aaah touche sir!

When i compile this code, i get the error:

c2664 'cood:: *function name*' : cannot convert parameter 1 from 'coord' to 'coord[]'

All i want to be able to do is pass the object into the function. if i remove the '[]' from the header files, i get errors telling me to remove all the '[]' from the implimentation cpp....then i know it wont work.

why exactly are you passing an array to your collision method? wouldn't you just want to pass one object or are you trying to see if everything in that array will collide with the calling object?

i want to see if the two objects have collided with each other.

okay so don't pass in an array of objects but pass in a single object. modify your coord class to just take in a single cord object in the collision function also changing the code to not use [] in your if statemnt. then in your main function just call asteroid[i].collision(asteroid[i+1]);

Adding a note ... you are doing some out-of-bounds access there, for example

// two 'coord' objects, meaning
// that you only can access indexes 0 and 1, nothing else.
coord asteroid[2];
...
//Collision check
for(int i = 0; i< 2; i++)
{
  asteroid[i].collision(asteroid[i+1]);
}

okay so don't pass in an array of objects but pass in a single object. modify your coord class to just take in a single cord object in the collision function also changing the code to not use [] in your if statemnt. then in your main function just call asteroid[i].collision(asteroid[i+1]);

Thanks for the quick help by the way, but i dont understand what your saying.
Now for *super double bonus annoying newbie time*
could you explain it to me in just a bit simpler language. Only been using classes for two weeks and im still not total with all the correct terms.

Much obliged

the function collision you have for your coord class right now is collision(coord[]). im saying just to make it collision(coord). then in the function re write your if statement to look like

if(this->xx == asteroid.getxx() && this->yy == asteroid.getyy())

the function collision you have for your coord class right now is collision(coord[]). im saying just to make it collision(coord). then in the function re write your if statement to look like

if(this->xx == asteroid.getxx() && this->yy == asteroid.getyy())

Cheers for the simpleton verson. It does work, just a few minor problems to solve that i should *hopefully* be able to work on my own.

Just so i learn something extra here, why when the object im passing is an array, does the function not care? spent 6 months learning C...dealing with pointers into functions. It just doesnt make sense to pass it without knowing its an array?!

because all an array is is a continuing block of data in memory. c/c++ doesn't care if its one or a million. the function only needs to know where the data starts at. you know its an array and can program accordingly for it.

because all an array is is a continuing block of data in memory. c/c++ doesn't care if its one or a million. the function only needs to know where the data starts at. you know its an array and can program accordingly for it.

I sort of understand what your saying, that the function doesnt care what it is....just where the data starts and ends.

But thank you for your time and im pleased to say the program now fully works!
I have left positive reputation...and now must struggle on with the next part of the question. Ta Muchly!

I sort of understand what your saying, that the function doesnt care what it is....just where the data starts and ends.

A function does not know where the data ends unless you tell it to the function. So if you pass an array into a function, be sure that the function also gets the information about the array's size (number of elements in it, that is).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.