Working out kinks on assignment to create custom vector class
6 replies, posted
I've been working on an assignment for my intro to programming for engineers class and have run into some trouble now that I'm "done" with it. To save some explaining, here are the objectives:
[code]Build a class called "vector" from the "point" class that we developed in class. The vector class will have these attributes:
a point that represents the start point of the vector
a point that represents the end point of the vector
either a point that represents the x, y, and z components of the vector (for example, the x components is the x coordinate of the end point minus the x coordinate of the start point) or separate double precision quantities for each component
a double precision value that represents the length
The vector class will have these methods:
constructors for a default vector (both the start point and the end point are the origin) and a vector with initial start and end points specified - note that these methods must also correctly set the components and the length
accessors that return the start point, end point, components, and length
mutators that set/change the start point and end point - note again that these methods must also correctly set the components and the length
adding two vectors (use operator overloading if desired) - this results in a third vector whose start point is set at the origin
subtracting two vectors (use operator overloading if desired) - this results in a third vector with start point at the origin
multiplying a vector by a scalar (double precision value; use operator overloading if desired) - results in a third vector whose start point you can set at the origin
dividing a vector by a scalar (use operator overloading if desired) - results in a third vector whose start point you can set at the origin
dot product between two vectors - results in a double precision value
cross vector between two vectors - results in a third vector whose start point you can set at the origin
creating a unit vector from an existing vector - set the start point of the unit vector to the origin
Once your class is constructed, creat a main function that does the following:
create three vectors with the following start and end points:
vector v1 - start (0,0,0), end (5,5,5)
vector v2 - start (-1,-2,-3), end (5,4,2)
vector v3 - start (-2,2,-3), end (-5,-4,7)
perform the operations v1+v2, v2-v3, v3-v1 and output the results
perform the operations v1*5 and v2/3 and output the results
calculate the dot products v1∙v2 and v2∙v3 and output the results
calculate the cross products v1×v3, v3×v1, v2×v3, and v3×v2 and output the results
create unit vectors out of v1, v2, and v3 and output the results
To output a vector, you can express each vector in terms of its components, for example, v5 = 2i + 3j - 5k.
[/code]
We went over most of this in class, but most of the other students did an alternate (and easier) assignment so it was just me and another student who ended up trying this one.
I'm getting a bunch of errors and I'm wondering if any of you guys can help me out with this stuff. All my code and the errors are below.
main assignment:
[cpp]/*
EGR126 Assignment 5
This program uses custom classes to calculate various operators on given vectors.
*/
#include "vector.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
point start1(0,0,0), end1(5,5,5), start2(-1,-2,-3), end2(5,4,2), start3(-2,2,-3), end3(-5,-4,7);
vector v1(start1,end1), v2(start2,end2), v3(start3,end3);
vector v1sumv2, v2minusv3, v3minusv1, v1multiplyA, v1divideB, v1crossv3, v3crossv1, v2crossv3, v3crossv2, v1unit, v2unit, v3unit;
double A = 5, B = 3, v1dotv2, v2dotv3;
v1sumv2 = v1 + v2;
v2minusv3 = v2 - v3;
v3minusv1 = v3 - v1;
v1multiplyA = v1*A;
v1divideB = v1/B;
v1dotv2 = v1.dot(v2);
v2dotv3 = v2.dot(v3);
v1crossv3 = v1.cross(v3);
v3crossv1 = v3.cross(v1);
v2crossv3 = v2.cross(v3);
v3crossv2 = v3.cross(v2);
v1unit = v1.unit(v1);
v2unit = v2.unit(v2);
v3unit = v3.unit(v3);
cout << "The result of vector 1 being added to vector 2 is: "<< endl;
cout << v1sumv2 << endl;
cout << "The result of vector 3 being subtracted from vector 2 is: "<< endl;
cout << v2minusv3 << endl;
cout << "The result of vector 1 being subtracted from vector 3 is: "<< endl;
cout << v3minusv1 << endl;
cout << "The result of vector 1 being multiplied by 5 is: "<< endl;
cout << v1multiplyA << endl;
cout << "The result of vector 1 being divided by 3 is: "<< endl;
cout << v1divideB << endl;
cout << "The dot product between vector 1 and vector 2 is: "<< endl;
cout << v1dotv2 << endl;
cout << "The dot product between vector 2 and vector 3 is: "<< endl;
cout << v2dotv3 << endl;
cout << "The cross product between vector 1 and vector 3 is: "<< endl;
cout << v1crossv3 << endl;
cout << "The cross product between vector 3 and vector 1 is: "<< endl;
cout << v3crossv1 << endl;
cout << "The cross product between vector 2 and vector 3 is: "<< endl;
cout << v2crossv3 << endl;
cout << "The cross product between vector 3 and vector 2 is: "<< endl;
cout << v3crossv2 << endl;
cout << "The unit vector of vector 1 is: "<< endl;
cout << v1unit << endl;
cout << "The unit vector of vector 2 is: "<< endl;
cout << v2unit << endl;
cout << "The unit vector of vector 3 is: "<< endl;
cout << v3unit << endl;
return 0;
}
[/cpp]
point.h
[cpp]class point
{
private:
double xCoord, yCoord, zCoord;
public:
double getx();
double gety();
double getz();
void setx(double newx);
void sety(double newy);
void setz(double newz);
double dist(point p);
point::point();
point::point(double x, double y, double z);
};[/cpp]
point.cpp
[cpp]#include <cmath>
#include "point.h"
using namespace std;
double point::getx()
{
return xCoord;
}
double point::gety()
{
return yCoord;
}
double point::getz()
{
return zCoord;
}
double point::setx(double newx)
{
xCoord = newx;
}
double point::sety(double newy)
{
yCoord = newy;
}
double point::setz(double newz)
{
zCoord = newz;
}
double point::dist(point p)
{
double t1, t2, t3, distance;
t1 = p.xCoord - xCoord;
t2 = p.yCoord - yCoord;
t3 = p.zCoord - zCoord;
distance = sqrt(t1*t1 + t2*t2 + t3*t3);
return distance;
}
point::point()
{
xCoord = yCoord = zCoord = 0;
}
point::point(double x, double y, double z)
{
xCoord = x;
yCoord = y;
zCoord = z;
}
[/cpp]
vector.h
[cpp]#include "point.h"
class vector
{
private:
point start, end;
double ihat, jhat, khat, length;
public:
point getStart();
point getEnd();
double getLength();
double getihat();
double getjhat();
double getkhat();
void setStart(point p);
void setEnd(point p);
vector vector::operator+(vector v);
vector vector::operator-(vector v);
vector vector::operator*(double n);
vector vector::operator/(double n);
double vector::dot(vector v);
vector vector::cross(vector v);
vector vector::unit(vector v);
vector::vector();
vector::vector(point startpt, point endpt);
};[/cpp]
vector.cpp
[cpp]#include <cmath>
#include "vector.h"
using namespace std;
point vector::getStart()
{
//point startpt;
return start;
}
point vector::getEnd()
{
//point endpoint;
return end;
}
double vector::getLength()
{
return length;
}
double vector::getihat()
{
return ihat;
}
double vector::getjhat()
{
return jhat;
}
double vector::getkhat()
{
return khat;
}
void vector::setStart(point p)
{
start = p;
length = start.dist(end);
ihat = start.getx() - end.getx();
jhat = start.gety() - end.gety();
khat = start.gety() - end.getz();
}
void vector::setEnd(point p)
{
end = p;
length = start.dist(end);
ihat = start.getx() - end.getx();
jhat = start.gety() - end.gety();
khat = start.getz() - end.getz();
}
vector vector::operator+(vector v)
{
/*vector sum;
point sumStart, sumEnd;
sum.getihat() = getihat() + v.getihat();
sumEnd.xCoord = sum.ihat;
sum.jhat = jhat + v.jhat;
sumEnd.yCoord = sum.jhat;
sum.khat = khat + v.khat;
sumEnd.zCoord = sum.khat;
sum.end = sumEnd;
return sum;*/
vector final;
point sum;
double x,y,z,length;
x = getihat() + v.getihat();
y = getjhat() + v.getjhat();
z = getkhat() + v.getkhat();
sum.setx(x);
sum.sety(y);
sum.setz(z);
final.setEnd(sum);
return final;
}
vector vector::operator-(vector v)
{
vector final;
point minus;
double x,y,z,length;
x = getihat() - v.getihat();
y = getjhat() - v.getjhat();
z = getkhat() - v.getkhat();
minus.setx(x);
minus.sety(y);
minus.setz(z);
final.setEnd(minus);
return final;
}
vector vector::operator*(double n)
{
vector final;
point multiply;
double x,y,z,length;
x = getihat() * n;
y = getjhat() * n;
z = getkhat() * n;
multiply.setx(x);
multiply.sety(y);
multiply.setz(z);
final.setEnd(multiply);
return final;
}
vector vector::operator/(double n)
{
vector final;
point divide;
double x,y,z,length;
x = getihat() / n;
y = getjhat() / n;
z = getkhat() / n;
divide.setx(x);
divide.sety(y);
divide.setz(z);
final.setEnd(divide);
return final;
}
double vector::dot(vector v)
{
//vector final;
//point multiply;
double x,y,z,length,dot;
x = getihat() * v.getihat();
y = getjhat() * v.getjhat();
z = getkhat() * v.getkhat();
/*multiply.setx(x);
multiply.sety(y);
multiply.setz(z);*/
//final.setEnd(multiply);
dot = x + y + z;
return dot;
}
vector vector::cross(vector v)
{
vector final;
point cross;
double x,y,z;
x = (getjhat()*v.getkhat()) - (getkhat()*v.getjhat());
y = (getihat()*v.getjhat()) - (getjhat()*v.getihat());
z = (getihat()*v.getkhat()) - (getkhat()*v.getihat());
cross.setx(x);
cross.sety(y);
cross.setz(z);
final.setEnd(cross);
return final;
}
vector vector::unit(vector v)
{
vector final;
point unit;
double x,y,z;
x = v.getihat()/length;
y = v.getjhat()/length;
z = v.getkhat()/length;
unit.setx(x);
unit.sety(y);
unit.setz(z);
final.setEnd(unit);
return final;
}
vector::vector()
{
point st, en;
start = st;
end = en;
length = 0;
}
vector::vector(point startpt, point endpt)
{
start = startpt;
end = endpt;
length = start.dist(end);
ihat = start.getx() - end.getx();
jhat = start.gety() - end.gety();
khat = start.getz() - end.getz();
}
[/cpp]
And the errors I'm getting:
[code]Warning 1 warning C4101: 'length' : unreferenced local variable c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\vector.cpp 66 1 Assignment 5
Warning 2 warning C4101: 'length' : unreferenced local variable c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\vector.cpp 84 1 Assignment 5
Warning 3 warning C4101: 'length' : unreferenced local variable c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\vector.cpp 102 1 Assignment 5
Warning 4 warning C4101: 'length' : unreferenced local variable c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\vector.cpp 120 1 Assignment 5
Warning 5 warning C4101: 'length' : unreferenced local variable c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\vector.cpp 138 1 Assignment 5
Error 6 error C2556: 'double point::setx(double)' : overloaded function differs only by return type from 'void point::setx(double)' c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\point.cpp 19 1 Assignment 5
Error 7 error C2371: 'point::setx' : redefinition; different basic types c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\point.cpp 19 1 Assignment 5
Error 8 error C2556: 'double point::sety(double)' : overloaded function differs only by return type from 'void point::sety(double)' c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\point.cpp 23 1 Assignment 5
Error 9 error C2371: 'point::sety' : redefinition; different basic types c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\point.cpp 23 1 Assignment 5
Error 10 error C2556: 'double point::setz(double)' : overloaded function differs only by return type from 'void point::setz(double)' c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\point.cpp 27 1 Assignment 5
Error 11 error C2371: 'point::setz' : redefinition; different basic types c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\point.cpp 27 1 Assignment 5
Error 12 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 37 1 Assignment 5
Error 13 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 39 1 Assignment 5
Error 14 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 41 1 Assignment 5
Error 15 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 43 1 Assignment 5
Error 16 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 45 1 Assignment 5
Error 17 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 51 1 Assignment 5
Error 18 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 53 1 Assignment 5
Error 19 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 55 1 Assignment 5
Error 20 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 57 1 Assignment 5
Error 21 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 59 1 Assignment 5
Error 22 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 61 1 Assignment 5
Error 23 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'vector' (or there is no acceptable conversion) c:\users\\documents\egr 126 assignments\ assignment 5\ assignment 5\assignment 5 - .cpp 63 1 Assignment 5
[/code]
A lot of it is probably inefficient, I know. But seeing as this is an intro class, I don't know anything prior.
You're implementation of setx() sety() and setz() return doubles even though you declared them as returning void in the header.
also almost all of your functions in vector.cpp redefine the variable "length" even though you declare it as a private variable in the header file.
the rest of the errors are because you are trying to print out a vector to cout. You probably should do something like:
cout << "The cross product between vector 3 and vector 2 is: "<< endl;
cout << "<" << v3crossv2.getx() << "," << v3crossv2.gety() << ", " << v3crossv2.getz() << "> " << endl;
Did you even try to fix some of the stuff yourself?
Edit:
I'm asking because part of learning is to fix your own problems. I'm learning a lot that way.
Since when do vectors have start and end points?
[QUOTE=ROBO_DONUT;33523077]Since when do vectors have start and end points?[/QUOTE]
i was just gonna mention that
creating a vector class with a concept of "start point" and "end point" is extremely confusing, especially considering most of the method end up setting the "start point" to (0, 0, 0) to begin with ...
[QUOTE=ROBO_DONUT;33523077]Since when do vectors have start and end points?[/QUOTE]
maybe he's confusing his vector with those vector arrows you draw in maths class
[QUOTE=swift and shift;33534626]maybe he's confusing his vector with those vector arrows you draw in maths class[/QUOTE]
Those vector arrows still only express magnitude and direction. Placement is arbitrary.
Sorry, you need to Log In to post a reply to this thread.