我剛剛開始使用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