当我尝试编译时遇到这些错误:
我非常努力地尝试解决所有错误,但那三个错误获胜了。我尝试了4个小时,因为我是初学者。我非常感谢你们的帮助。
1)
Error 1 error LNK2005: "class std::basic_istream > & __cdecl cs52::operator>>(class std::basic_istream > &,class cs52::SwimmingPool &)" (??5cs52@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVSwimmingPool@0@@Z) already defined in POOL.obj
2)
Error 2 error LNK2005: "class std::basic_ostream > & __cdecl cs52::operator<<(class std::basic_ostream > &,class cs52::SwimmingPool const &)" (??6cs52@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABVSwimmingPool@0@@Z) already defined in POOL.obj
3)
Error 3 error LNK1169: one or more multiply defined symbols found
这是我的代码:
1)标题
#ifndef POOL_H
#define POOL_H
#include<iostream>
namespace cs52
{
class SwimmingPool{
public:
SwimmingPool(int size);
void fill(int amount);
void splash();
void swim();
void evaporate(int amount);
int getSize();
int getContents();
void setSize(int size);
private:
int mySize; // in gallons
int myContents; // in gallons
friend std::ostream& operator<<(std::ostream& outs, const SwimmingPool & pool);
friend std::istream& operator>>(std::istream& ins, SwimmingPool & pool);
friend SwimmingPool operator +(SwimmingPool& d, SwimmingPool& d2);
friend SwimmingPool operator -(SwimmingPool& d, SwimmingPool& d2);
};
bool operator >(SwimmingPool& d, SwimmingPool& d2);
bool operator <(SwimmingPool& d, SwimmingPool& d2);
bool operator ==(SwimmingPool& d, SwimmingPool& d2);
bool operator !=(SwimmingPool& d, SwimmingPool& d2);
std::ostream& operator<<(std::ostream& outs, const SwimmingPool & pool){
outs << "size " << pool.mySize << " contents " << pool.myContents;
return outs;
}
std::istream& operator>> (std::istream& ins, SwimmingPool & pool)
{
std::cout << "enter size of pool\n";
ins >> pool.mySize;
pool.myContents = 0;
return ins;
}
}
#endif
2)这是我的第一个cpp文件
#include "POOL.h"
using namespace std;
using namespace cs52;
SwimmingPool::SwimmingPool(int size)
{
mySize = size;
myContents = 0;
}
void SwimmingPool::fill(int amount)
{
myContents += amount;
}
void SwimmingPool::splash()
{
cout << "splash" << std::endl;
}
void SwimmingPool::evaporate(int amount)
{
myContents -= amount;
}
int SwimmingPool::getSize()
{
return mySize;
}
int SwimmingPool::getContents()
{
return myContents;
}
void SwimmingPool::swim()
{
cout << "swimming\n";
}
bool cs52::operator >(SwimmingPool& d, SwimmingPool& d2)
{
if (d2.getSize() <d.getSize())
{
return true;
}
return false;
}
bool cs52::operator <(SwimmingPool& d, SwimmingPool& d2)
{
if (d2.getSize() >d.getSize())
{
return true;
}
return false;
}
bool cs52::operator ==(SwimmingPool& d, SwimmingPool& d2)
{
if (d2.getSize() == d.getSize())
{
return true;
}
return false;
}
bool cs52::operator !=(SwimmingPool& d, SwimmingPool& d2)
{
if (d2.getSize() != d.getSize())
{
return true;
}
return false;
}
SwimmingPool cs52::operator +(SwimmingPool& d, SwimmingPool& d2)
{
SwimmingPool s(d.getSize() + d2.getSize());
s.fill(d.getContents() + d2.getContents());
return s;
}
SwimmingPool cs52::operator -(SwimmingPool& d, SwimmingPool& d2)
{
if (d.getSize() >= d2.getSize() && d.getSize() >= d2.getSize())
{
SwimmingPool s(d.getSize() - d2.getSize());
s.fill(d.getContents() - d2.getContents());
return s;
}
else
cout << "ERROR\n";
return 0;
}
3) 这是我的主 cpp 文件
#include "POOL.h"
#include<iostream>
using namespace std;
using namespace cs52;
int main()
{
SwimmingPool smallOne(1);
SwimmingPool bigOne(1000);
bigOne.fill(100);
SwimmingPool yours(10);
yours.fill(1);
SwimmingPool mine(20);
mine.fill(19);
cout << "--some tests follow--" << endl;
SwimmingPool pool1 = mine + mine;
SwimmingPool pool2 = yours - yours;
if (pool1 > pool2) {
cout << "> is working..." << endl;
}
if (pool1 != pool2) {
cout << "!= is working..." << endl;
}
if (pool2 < pool1) {
cout << "< is working..." << endl;
}
if (pool1 == pool1) {
cout << "== is working..." << endl;
}
cout << "---printing pool1---" << endl;
cout << pool1 << endl;
cout << "---printing pool2---" << endl;
cout << pool2 << endl;
cout << "---reading pool1---" << endl;
cin >> pool1;
cout << "---printing a revised pool1---" << endl;
cout << pool1 << endl;
cout << "---some broken code follows---" << endl;;
SwimmingPool badPool = smallOne - bigOne;
system("pause");
return 0;
}
提前致谢,
最佳答案
你定义你operator <<
和operator >>
头文件中的自由函数。这会导致实现被编译到两个目标文件中,从而导致重复的定义。
您必须:
- 将它们声明为
inline
- 将实现移至 CPP 文件中
- 使重载方法成为类的成员,并直接在类定义主体中实现它们(这实际上使它们成为
inline
)
选项 1 导致编译器不定义该函数,而是将其实现复制到调用它的任何地方。当它是编译到 DLL 中的 API 的一部分时,这可能会导致问题。交换 DLL 时,函数的更改不会影响现有程序,因为实现位于主工件中(二进制兼容性被破坏)
我认为选项 2 是最好的选择,因为它将运算符实现绑定(bind)到类实现
选项 3 在您的情况下不起作用,因为您的类必须是运算符的 LHS(运算符重载方法将在您的类的对象上调用)。输入/提取运算符的情况并非如此。
关于c++ - 链接器错误(lnk2005、lnk1169),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23884527/