Reputation: 17
I am getting these errors when I try to compile:
I tried really hard to resolve all the errors, but those three won. I tried for 4 hours because I am a beginner. I would really appreciate your help guys.
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
This is my code:
1)Header
#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) This is my first cpp file
#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) This is my Main cpp file
#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;
}
Thanks in advance,
Upvotes: 2
Views: 1085
Reputation: 11513
You define you operator <<
and operator >>
free functions in your header file. This causes that the implementation is compiled into both object files, thus resulting in duplicate definitions.
You have to either:
inline
inline
)Option 1 causes the compiler to not define the function, but instead copy its implementation to wherever it is called. This can cause problems when it is part of an API compiled into a DLL. Changes in the function will not affect existing programs when the DLL is exchanged, since the implementation is in the main artefact (binary compability is broken)
Option 2 is the best option in my opinion, as it binds operator implementation to the class implementation
Option 3 will not work in your case, because then your class must be the LHS of the operator (the operator overload method will be called on object of your class). This is not the case for input/extraction operators.
Upvotes: 5