I've seen a few related posts, but I can't really make sense of what I need to do to fix a program I'm making for my entry-level C++ class.
My errors are:
Build Final Project of project Final Project with configuration Debug
Ld "build/Debug/Final Project" normal x86_64
cd "/Users/nick/Dropbox/|Syncs/Xcode/Final Project"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk "- L/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug" "-F/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug" -filelist "/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Final Project.build/Debug/Final Project.build/Objects-normal/x86_64/Final Project.LinkFileList" -mmacosx-version-min=10.6 -o "/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug/Final Project"
Undefined symbols:
"Vector<double>::Vector()", referenced from:
_main in main.o
"Vector<double>::length()", referenced from:
_main in main.o
"Vector<double>::Vector(double const&, double const&, double const&)", referenced from:
_main in main.o
_main in main.o
"Vector<double>::getx() const", referenced from:
_main in main.o
_main in main.o
"Vector<double>::gety() const", referenced from:
_main in main.o
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Here's what I've got:
//main.cpp
#include <iostream>
#include "Vector.h"
int main () {
Vector<double> a(1, 2, 3);
Vector<double> b(2, 4, 4);
Vector<double> c;
std::cout << "Length: " << b.length() << std::endl;
std::cout << b.getx() << " ," << b.gety() << std::endl;
std::cout << c.getx() << " , " << c.gety() << std::endl;
return 0;
}
and
//Vector.h
template <class T>
class Vector {
T x, y, z;
public:
//constructors
Vector();
Vector(const T& x, const T& y, const T& z);
Vector(const Vector& u);
//accessors
T getx() const;
T gety() const;
T getz() const;
//mutators
void setx(const T& x);
void sety(const T& y);
void setz(const T& z);
//operations
void operator-();
Vector plus(const Vector& v);
Vector minus(const Vector& v);
Vector cross(const Vector& v);
T dot(const Vector& v);
void times(const T& s);
T length();
};
and the Vector.cpp (though I've trimmed some code that's somewhat duplicative – accessors & mutators for y and z, for instance)
//Vector.cpp
#include "Vector.h"
#include <iostream>
#include <math.h>
template class Vector<int>;
template class Vector<double>;
//default constructor
template <class T>
Vector<T>::Vector(): x(0), y(0), z(0) {}
//constructor
template <class T>
Vector<T>::Vector(const T& x, const T& y, const T& z)
{
setx(x);
sety(y);
setz(z);
}
//copy constructor
template <class T>
Vector<T>::Vector(const Vector& u)
{
x = u.getx();
y = u.gety();
z = u.getz();
}
//x accessor
template <class T>
T Vector<T>::getx() const
{
return x;
}
//y accessor
//z accessor
//x mutator
template <class T>
void Vector<T>::setx(const T& x)
{
Vector::x = x;
}
//y mutator
//z mutator
//negated Vector
template <class T>
void Vector<T>::operator-()
{
setx(-this->getx());
sety(-this->gety());
setz(-this->getz());
}
//sum
template <class T>
Vector<T> Vector<T>::plus(const Vector& v)
{
Vector ret((getx() + v.getx()), (gety() + v.gety()), (getz() + v.getz()));
return ret;
}
//difference
template <class T>
Vector<T> Vector<T>::minus(const Vector& v)
{
Vector ret((getx() - v.getx()), (gety() - v.gety()), (getz() - v.getz()));
return ret;
}
//cross product
template <class T>
Vector<T> Vector<T>::cross(const Vector& v)
{
Vector ret;
ret.setx(gety()*v.getz() - getz()*v.gety());
ret.sety(getz()*v.getx() - getx()*v.getz());
ret.setz(getx()*v.gety() - gety()*v.getx());
return ret;
}
//dot product
template <class T>
T Vector<T>::dot(const Vector& v)
{
return (getx()*v.getx() + gety()*v.gety() + getz()*v.getz());
}
//scalar times Vector
template <class T>
void Vector<T>::times(const T& s)
{
setx(getx()*s);
sety(gety()*s);
setz(getz()*s);
}
//length of Vector
template <class T>
T Vector<T>::length()
{
return sqrt((this->dot(*this)));
}
So, what the hell's going on? Is it because I have a separate header & .cpp file for Vector? How do I get my main function to recognize functions of my Vector class?