我收到许多未解决的符号错误,但我找不出原因。Visual Studio无法找到我的方法,尽管它们拼写正确
此问题的最常见来源似乎拼写错误的方法名称,忘记使用作用域操作符指示方法属于哪个类,并忘记将.cpp
文件包含在项目中。
虽然我已经检查了所有这些问题,但它们都不适用。我告诉它方法属于哪个类(通过Point<T>:: ...
),我已经双重检查了方法名称,并且它们都是正确的,并且.cpp
文件显示在解决方案资源管理器中(我甚至尝试删除它并重新 - 加入)。
我有一段时间没有编写C++,所以我可能会忽略某些东西,但我看不到什么。 (发布后,我意识到我的操作员已经被打乱了,但这不应该影响我的错误,所以请暂时忽略)。
具体而言,这些是我得到的错误:
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Point<int>::Point<int>(int,int)" ([email protected]@@[email protected]@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator+(class Point<int>)const " ([email protected]@@[email protected]@@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator-(class Point<int>)const " ([email protected]@@[email protected]@@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator*(class Point<int>)const " ([email protected]@@[email protected]@@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator/(class Point<int>)const " ([email protected]@@[email protected]@@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point<int>)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@@@Z) referenced in function _main
而这些都是有问题的文件:
Point.h
#ifndef POINT_H
#define POINT_H
#include <iostream>
template <typename T>
class Point {
const T x;
const T y;
public:
Point(T x, T y);
Point<T> moveBy(T xOff, T yOff) const;
Point<T> operator+(const Point<T> otherPoint) const;
Point<T> operator-(const Point<T> otherPoint) const;
Point<T> operator*(const Point<T> otherPoint) const;
Point<T> operator/(const Point<T> otherPoint) const;
friend std::ostream& operator<<(std::ostream& os, const Point<T> p);
private:
Point<T> applyOperator(T(*f)(T, T), const Point<T> otherPoint) const;
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const Point<T> p) {
return os << "(" << p.x << "," << p.y << ")";
}
#endif
Point.cpp:
#include "Point.h"
template <typename T>
Point<T>::Point(T x, T y) :
x(x),
y(y) {
}
template <typename T>
Point<T> Point<T>::moveBy(T xOff, T yOff) const {
return this + Point(xOff, yOff);
}
template <typename T>
Point<T> Point<T>::operator+(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x + y});
}
template <typename T>
Point<T> Point<T>::operator-(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x - y});
}
template <typename T>
Point<T> Point<T>::operator*(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x * y});
}
template <typename T>
Point<T> Point<T>::operator/(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x/y});
}
template <typename T>
Point<T> Point<T>::applyOperator(T(*f)(T, T), const Point<T> otherPoint) const {
return Point(f(this.x, otherPoint.x), f(this.y, otherPoint.y));
}
An d的Main.cpp的:
#include <iostream>
#include "Point.h"
int main(int argc, char* argv[]) {
Point<int> p1(1, 2);
Point<int> p2(2, 3);
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
Point<int> p3 = p1 + p2;
Point<int> p4 = p1 - p2;
Point<int> p5 = p1 * p2;
Point<int> p6 = p1/p2;
std::cout << p3 << std::endl;
std::cout << p4 << std::endl;
std::cout << p5 << std::endl;
std::cout << p6 << std::endl;
}
WOW。我简直不敢相信我忘了!非常感谢你。 – Carcigenicate