`

C++ 问题集

    博客分类:
  • C++
阅读更多

1. undefined reference to 'std::cout'错误

这是由于用gcc编译带来的问题,没有链接。 C++程序应该用g++命令编译,它会自动调用gcc来编译并链接。
而对于C++程序,gcc命令只能编译,不能完成库的链接。

 

2. C++标准规定:非const的引用不能加在临时变量上(避免临时对象销毁之后引用无效),问题:
void fun2(string &str)
{
    cout << str << endl;
}

int main()
{
    fun2(string("hello, "));
}
gcc编译错误提示如下:
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp: In function `int main()':
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp:35: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp:28: error: in passing argument 1 of `void fun2(std::string&)'

 

解决方法吧:
1` 在参数上加上const.
2` 没有初始化,因为没有自己的构造函数,默认构造函数不做任何初始化工作. 初始化变量

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics