首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链接器错误(link2005,link1169)

链接器错误(link2005,link1169)
EN

Stack Overflow用户
提问于 2014-05-27 08:27:55
回答 1查看 953关注 0票数 2

当我试图编译时,我得到了这些错误:

我非常努力地解决所有的错误,但这三个赢了。我试了4个小时,因为我是个初学者。我非常感谢你们的帮助。

1)

错误1错误(??5cs52@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVSwimmingPool@0@@Z):"class std::basic_istream >& __cdecl cs52::operator>>(类std::basic_istream > &,类in 52::泳客&)“已在POOL.obj中定义

2)

错误2错误LNK2005:"class std::basic_ostream >& __cdecl cs52::operator<<(class std::basic_ostream > &,class in 52::泳池const &)“已在POOL.obj中定义

3)

错误3错误LNK1169:找到一个或多个被多重定义的符号

这是我的密码:

1)报头

代码语言:javascript
复制
#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文件

代码语言:javascript
复制
#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文件

代码语言:javascript
复制
#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;
}

提前谢谢你,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-27 08:35:14

在头文件中定义operator <<operator >>空闲函数。这将导致将实现编译到两个对象文件中,从而导致定义重复。

你必须要么:

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

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

在我看来,选项2是最好的选择,因为它将操作符实现绑定到类实现。

选项3在您的情况下不起作用,因为您的类必须是操作员的LHS (将在类的对象上调用操作符重载方法)。对于输入/提取操作符来说,情况并非如此。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23884527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档