Tôi đã xem một số related posts, nhưng tôi thực sự không thể hiểu được những gì tôi cần làm để sửa chương trình tôi đang tạo cho lớp C++ cấp nhập cảnh của mình.Lớp mẫu - Ký hiệu không được tìm thấy
lỗi của tôi là:
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
Đây là những gì tôi đã có:
//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;
}
và
//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();
};
và Vector.cpp (mặc dù tôi đã cắt một số mã đó là hơi trùng lắp - các trình biến đổi & biến tần cho y và z, ví dụ)
//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)));
}
Vậy, cái quái gì đang xảy ra? Có phải vì tôi có tiêu đề riêng biệt & tệp .cpp cho Vector không? Làm thế nào để tôi nhận được chức năng chính của mình để nhận ra các chức năng của lớp Vector của tôi?
Một nguồn thông tin là [C++ FAQ] (http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12), một [sách] tốt (http: // stackoverflow .com/questions/388242/the-definitive-c-book-guide-and-list) thích * "Mẫu - Hướng dẫn đầy đủ" * cũng sẽ hữu ích. –