A simple function

//In the Second function: I want "_ax" to change a1, a2, a3, a4...a11
//Is there a way to make 1 function instead of 11?
//Collisions\main.cpp|79|error: '_ax' has not been declared|

class obj {
public:
struct axis
{
bool collide; //0 - Ignore Collisions; 1 - Normal Collision;
double position; //Base of Collisions Calculations
double velocity; //Transotion in 1 tick
double width; //Space taken on axis
double high; //Highest Point on axis
double low; //Lowest Point on axis
};
axis a1;
axis a2;
axis a3;
axis a4;
char* name;
void update();
void setCollide(bool, bool, bool, bool);
void setPos(double, double, double, double);
void setVel(double, double, double, double);
void setWidth(double, double, double, double);
};

...

bool Collideax (bool debug, _ax, objA, objB) { // I want "_ax" to change a1, a2, a3, a4...a11
if (objA._ax.collide == false || objB._ax.collide == false ) {
if (debug == true) {
outputfile
<< _ax
<< " Collide"
<< (objA._ax.collide == false)
<< (objB._ax.collide == false)
<< endl;}
return true;
} else {
if (objA._ax.low <= objB._ax.high && objA._ax.low >= objB._ax.low || objA._ax.high <= objB._ax.high && objA._ax.high >= objB._ax.low) //Test a1
{
if (debug == true) {
cout << "Collision Detected " << _ax << " - " << objA.name << " and " << obj2.name << endl<<
"Time - " << ticks << endl <<
"Position ObjA " << _ax << " Position - " << (objA._ax.position) << endl <<
"Position ObjB " << _ax << " Position - " << (objB._ax1.position) << endl;
outputfile << "Collision Detected " << _ax << " - " << objA.name << " and " << objB.name << endl <<
"Time - " << ticks << endl <<
"Position ObjA " << _ax << " Position - " << (objA._ax.position) << endl <<
"Position ObjB " << _ax << " Position - " << (objB._ax.position) << endl
<< ")" << endl;
}
return true;
} else {
return false;
}
}
}

int time () {
while (ticks < 100) {
//move objects
outputfile << "Tick " << ticks << " - ";
obj1.update();
obj2.update();

//test collidion
bool colla1 = Collideax(1, a1, obj1, obj2); //obj9285...
bool colla2 = Collideax(1, a2, obj1, obj2);
bool colla3 = Collideax(1, a3, obj1, obj2);
bool colla4 = Collideax(1, a4, obj1, obj2);
if (colla1 && colla2 && colla3 && colla4) {
cout<< "Collide" << endl;
outputfile << colla1 << colla2 << colla3 << colla4 << endl;
} else {
cout << colla1 << colla2 << colla3 << colla4 << endl;
outputfile << colla1 << colla2 << colla3 << colla4 << endl;
}
ticks++;
Sleep(1);
}
cout << endl << "Ticks - " << ticks << endl;
outputfile << endl << "Ticks - " << ticks << endl;
return (ticks);
}


int main(){
obj1.setPos(10,-10,0,.2);
obj1.setVel(-.2,.2,0,0);
obj1.setWidth(1,1,1,.4);
obj1.setCollide(true, true, false, false);
obj1.name = "Box One";
obj1.update();
obj2.setPos(-10,10,0,0);
obj2.setVel(.2,-.2,0,0);
obj2.setWidth(1,1,1,0);
obj2.setCollide(true, true, false, false);
obj2.name = "Box Two";
obj2.update();
time();
outputfile.close();
}

//In the Second function: I want "_ax" to change a1, a2, a3, a4...a11
//Is there a way to make 1 function instead of 11?
//Collisions\main.cpp|79|error: '_ax' has not been declared|
Why not make a1...11 an array and pass the subscript to the function?
how do you "make a1...11 an array and pass the subscript to the function?"
Check you this tutorial on arrays:
http://cplusplus.com/doc/tutorial/arrays/
You seem to already know how to pass values to functions, but you can review that here as well:
http://cplusplus.com/doc/tutorial/functions/
i was looking for something like:
int _ax
float _ax
double _ax
...

i don't see how an array will help with out it ether
An array will help because you'll need only one function to work on that array; you'd simply pass which element of that array you want the function to work on. In any case I'd suggest making large numbers of variables that simply have a different number appended to them an array anyway as it makes programmatical sense since they are all related.

If you're set on trying to use a sort of _ax thing, you could make those variables reference parameters to the function and pass in the particular a1...11 you want as an argument.
1
2
3
4
5
6
7
8
9
void func(int& _ax) {
    _ax += 1;
}

int main() {
    int a1, a2, a3; // ... assume defined
    func(a1); // func() will use _ax to refer to a1
    func(a2); //etc.
}

Zunft wrote:
i was looking for something like:
int _ax
float _ax
double _ax

For this, you'd want to use function templates: http://cplusplus.com/doc/tutorial/templates/

A simple template to add arguments of different types:
1
2
3
4
5
template <class A, class B>
A add (A a, B b)
{
	return a + b;
}


You can substitute types A and B with whatever you need to add.
Last edited on
Topic archived. No new replies allowed.