请解释此程序的错误消息..
#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'设置但不是 used [-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退出状态
答案 0 :(得分:1)
使用g++
进行编译,而不是gcc
。你有C ++代码,而不是C代码。
它与类代码无关。
答案 1 :(得分:-1)
有两种警告信息。第二个是因为gcc中缺少链接标志:
gcc -lstdc++ -Wall -o "untitled" "untitled.cpp"
(或等效的g++ -Wall -o "untitled" "untitled.cpp
。
关于未使用变量的第一个警告是因为obj3
变量已声明但未在其他任何地方使用。对于这种情况,我使用(void)obj3;
语句来解决此类警告消息。