【问题标题】:About assignment operator over loading error关于赋值运算符重载错误
【发布时间】:2012-05-26 15:49:46
【问题描述】:

请解释这个程序的错误信息..

#include <iostream>
using namespace std;
class copyConst
{

    private:
    int someVal;
    public:
    copyConst(const copyConst &objParam)
    {
        someVal = objParam.someVal;
    }
    copyConst()
    {
        someVal = 9;
    }
    copyConst& operator=(const copyConst &objParam)
    {
        if (this == &objParam)
            return *this;

        someVal = objParam.someVal;
        return *this;
        }
    };

int main(int argc, char **argv)
{
    copyConst obj1;
    copyConst obj2(obj1);
    copyConst obj3 = obj1;
    copyConst obj4;
    obj4 = obj1;


    return 0;
}

错误信息:

gcc -Wall -o "untitled" "untitled.cpp" (在目录中: /home/rwik/Documents) untitled.cpp: 在函数'int main(int, char**)’: untitled.cpp:53:12: 警告: 变量‘obj3’设置但没有 使用 [-Wunused-but-set-variable] /tmp/ccUIyRPg.o:在函数中 __static_initialization_and_destruction_0(int, int)': untitled.cpp:(.text+0x8a): undefined reference to std::ios_base::Init::Init()' untitled.cpp:(.text+0x8f): undefined 引用 `std::ios_base::Init::~Init()' 编译失败。 collect2: ld 返回 1 个退出状态

【问题讨论】:

    标签: c++ copy-constructor


    【解决方案1】:

    使用g++ 编译,而不是gcc。你有 C++ 代码,而不是 C 代码。

    与类代码无关。

    【讨论】:

    • 奇怪的downvote,答案完全正确! +1 补偿。
    【解决方案2】:

    有两种类型的警告消息。第二个是因为 gcc 中缺少链接标志: gcc -lstdc++ -Wall -o "untitled" "untitled.cpp"(或等效的g++ -Wall -o "untitled" "untitled.cpp

    关于未使用变量的第一个警告是因为 obj3 变量已声明但未在其他任何地方使用。对于这种情况,我使用(void)obj3; 语句来解决此类警告消息。

    【讨论】:

      猜你喜欢
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2013-02-14
      • 2016-08-30
      • 1970-01-01
      相关资源
      最近更新 更多