gpt4 book ai didi

c++ - 链接器错误(lnk2005、lnk1169)

转载 作者:搜寻专家 更新时间:2023-10-31 00:35:26 27 4
gpt4 key购买 nike

我在尝试编译时遇到这些错误:

我非常努力地解决了所有错误,但那三个人赢了。我试了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 >>头文件中的自由函数。这导致实现被编译到两个目标文件中,从而导致重复定义。

您必须:

  1. 将它们声明为 inline
  2. 在 CPP 文件中移动实现
  3. 使重载方法成为您类的成员并直接在类定义体中实现它们(这实际上使它们成为 inline )

选项 1 导致编译器不定义函数,而是将其实现复制到调用它的任何地方。当它是编译成 DLL 的 API 的一部分时,这可能会导致问题。交换 DLL 时,函数的更改不会影响现有程序,因为实现在主要工件中(二进制兼容性被破坏)

选项 2 在我看来是最好的选择,因为它将运算符实现绑定(bind)到类实现

选项 3 不适用于您的情况,因为您的类必须是运算符的 LHS(将在您的类的对象上调用运算符重载方法)。输入/提取运算符不是这种情况。

关于c++ - 链接器错误(lnk2005、lnk1169),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23884527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com