Im trying to pass my own class to itself but it isn't working. (all the errors came when I added the add function)
[cpp]
// Main.cpp
#include <iostream>
#include "vectors.h"
int main()
{
vector vector1 (0,50,10);
vector vector2;
vector1 = vector.add(vector1,vector2);
vector1.Vprint();
std::cin.get();
return 0;
}
// Header
#include <iostream>
#include <string>
class vector
{
int x,y,z;
public:
vector add(vector, vector);
vector();
vector(int, int, int);
void Vprint();
};
vector vector::add(vector1, vector2)
{
vector add;
add.x = vector1.x + vector2.x;
add.y = vector1.y + vector2.y;
add.z = vector1.z + vector2.z;
return add;
}
vector::vector()
{
x = 0;
y = 0;
z = 0;
}
vector::vector(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
void vector::Vprint()
{
std::cout << "Current coordinates :" << x << "," << y << "," << z << "\n";
}
[/cpp]
Errors :
1>c:\users\quincy\documents\visual studio 2008\projects\vector\vector\vectors.h(14) : error C2065: 'vector1' : undeclared identifier
1>c:\users\quincy\documents\visual studio 2008\projects\vector\vector\vectors.h(14) : error C2065: 'vector2' : undeclared identifier
1>c:\users\quincy\documents\visual studio 2008\projects\vector\vector\vectors.h(15) : error C2448: 'vector::add' : function-style initializer appears to be a function definition
1>c:\users\quincy\documents\visual studio 2008\projects\vector\vector\main.cpp(8) : warning C4832: token '.' is illegal after UDT 'vector'
1> c:\users\quincy\documents\visual studio 2008\projects\vector\vector\vectors.h(5) : see declaration of 'vector'
1>c:\users\quincy\documents\visual studio 2008\projects\vector\vector\main.cpp(8) : error C2275: 'vector' : illegal use of this type as an expression
1> c:\users\quincy\documents\visual studio 2008\projects\vector\vector\vectors.h(5) : see declaration of 'vector'
Line 29: you're not actually specifying the argument types.
Also, you should pass your vectors by [b]reference to const[/b], since you are not modifying the values and do not need to pass a copy.
This can speed up program execution significantly with bigger objects (or many operations to smaller objects). Instead of copying the whole object, you pass its memory address, like this:
[cpp]vector vector::add(const vector& vector1, const vector& vector2)
{
vector add;
add.x = vector1.x + vector2.x; // Here we are working with the original vectors you passed to the function, not copies.
/* etc. */
}[/cpp]
Also there's quite a few things you could fix in your class (make add a static function or replace it by operator+) but I'll let it slide for now while you experiment. Somebody might bring it up if they particularly care.
[QUOTE=Nevec;16754499]Line 29: you're not actually specifying the argument types.[/QUOTE]
What do you mean ?
This is Line 29:
[cpp]vector vector::add(vector1, vector2)[/cpp]
These are arguments:
vector vector::add[b](vector1, vector2)[/b]
These are arguments along with their type:
vector vector::add[b](vector vector1, vector vector2)[/b]
What about this :
1>c:\users\quincy\documents\visual studio 2008\projects\vector\vector\main.cpp(8) : warning C4832: token '.' is illegal after UDT 'vector'
1> c:\users\quincy\documents\visual studio 2008\projects\vector\vector\vectors.h(5) : see declaration of 'vector'
1>c:\users\quincy\documents\visual studio 2008\projects\vector\vector\main.cpp(8) : error C2275: 'vector' : illegal use of this type as an expression
1> c:\users\quincy\documents\visual studio 2008\projects\vector\vector\vectors.h(5) : see declaration of 'vector'
Newcode :
[cpp]
#include <iostream>
#include "vectors.h"
int main()
{
vector vector1 (0,50,10);
vector vector2;
vector1 = vector.add(vector1,vector2);
vector1.Vprint();
std::cin.get();
return 0;
}
// Header
#include <iostream>
#include <string>
class vector
{
int x,y,z;
public:
vector add(vector, vector);
vector();
vector(int, int, int);
void Vprint();
};
vector vector::add(vector vector1, vector vector2)
{
vector add;
add.x = vector1.x + vector2.x;
add.y = vector1.y + vector2.y;
add.z = vector1.z + vector2.z;
return add;
}
vector::vector()
{
x = 0;
y = 0;
z = 0;
}
vector::vector(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
void vector::Vprint()
{
std::cout << "Current coordinates :" << x << "," << y << "," << z << "\n";
}
[/cpp]
I think its because you cant just change a object like variables but I don't know how to fix it.
Line 8: You can't explicitly call vector.add(), you would need to do vector1.add() or vector2.add() (unless you made it a static function).
ahhh I see. Thanks
Sorry, you need to Log In to post a reply to this thread.