我刚刚开始使用C++,也许有些事情我在这里做错了,但我很茫然。当我尝试构建解决方案,我得到这样一个4个LNK2005
错误:Visual Studio 2010链接器查找乘法定义的符号(它不应该在那里)
error LNK2005: "public: double __thiscall Point::GetX(void)const " ([email protected]@@QBENXZ) already defined in CppSandbox.obj
(有一个每个的get/set方法,他们都涉嫌发生在Point.obj
)
然后最后这个错误:
error LNK1169: one or more multiply defined symbols found
据称发生在CppSandbox.exe
。我不确定是什么原因导致了这个错误 - 当我构建或重建解决方案时,似乎发生了这种情况......说实话,真的很茫然。
以下三个文件是我添加到默认VS2010空白项目的全部内容(它们完整地复制)。谢谢你尽你所能的帮助。
Point.h
class Point
{
public:
Point()
{
x = 0;
y = 0;
};
Point(double xv, double yv)
{
x = xv;
y = yv;
};
double GetX() const;
void SetX(double nval);
double GetY() const;
void SetY(double nval);
bool operator==(const Point &other)
{
return GetX() == other.GetX() && GetY() == other.GetY();
}
bool operator!=(const Point &other)
{
return !(*this == other);
}
private:
double x;
double y;
};
Point.cpp
#include "Point.h"
double Point::GetX() const
{
return x;
}
double Point::GetY() const
{
return y;
}
void Point::SetX(double nv)
{
x = nv;
}
void Point::SetY(double nv)
{
y = nv;
}
CppSandbox.cpp
// CppSandbox.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "Point.cpp"
int main()
{
Point p(1, 2);
Point q(1, 2);
Point r(2, 3);
if (p == q) std::cout << "P == Q";
if (q == p) std::cout << "Equality is commutative";
if (p == r || q == r) std::cout << "Equality is broken";
return 0;
}
修复了它,我添加了包含警卫。谢谢你的帮助 – ehdv 2010-04-20 19:32:01