C++ 链接器错误(link2005、link1169)
我在尝试编译时遇到以下错误: 我真的很努力地解决所有的错误,但那三个赢了。我试了4个小时,因为我是初学者。我非常感谢你们的帮助 (一) 错误1错误LNK2005:“类std::basic_istream>&类cdecl cs52::operator>>(类std::basic_istream>&类cs52::SwimmingPool&)”(?5cs52@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVSwimmingPool@0@@Z)已在POOL.obj中定义 (二)C++ 链接器错误(link2005、link1169),c++,C++,我在尝试编译时遇到以下错误: 我真的很努力地解决所有的错误,但那三个赢了。我试了4个小时,因为我是初学者。我非常感谢你们的帮助 (一) 错误1错误LNK2005:“类std::basic_istream>&类cdecl cs52::operator>>(类std::basic_istream>&类cs52::SwimmingPool&)”(?5cs52@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVSwimmingPool
错误2错误LNK2005:“类std::basic_ostream>和&uu cdecl cs52::operator正在工作…”您在头文件中定义了
operator
自由函数。这导致实现被编译到两个对象文件中,从而导致重复的定义
你必须:
inline
选项3在您的情况下不起作用,因为这样您的类必须是操作符的LHS(操作符重载方法将在您的类的对象上调用)。输入/提取运算符不是这样。顺便说一句,您的实现也应该与声明在同一个名称空间中,所以在您的第一个cpp文件中这样做:名称空间cs52{//your code here}非常感谢您,我的代码终于活了起来,感谢您:D
#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
#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;
}
#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;
}