rah*_*man 10 c++ linux constructor
有这样的程序:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
test(std::string s):str(s){};
private:
std::string str;
};
class test1
{
public:
test tst_("Hi");
};
int main()
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
...为什么我执行时会得到以下内容
g ++ main.cpp
main.cpp:16:12: error: expected identifier before string constant
main.cpp:16:12: error: expected ‘,’ or ‘...’ before string constant
Run Code Online (Sandbox Code Playgroud)
izo*_*ica 17
您无法在声明它的地方初始化tst_.这只能用于静态const基元类型.相反,你需要有test1的构造函数.
编辑:这是ideone.com中的一个工作示例.注意我做了一些更改 - 首先,让测试的构造函数对字符串进行const引用以避免复制更好.第二 - 如果程序成功,你应该返回0而不是1(返回1你在ideone中得到运行时错误)
| 归档时间: |
|
| 查看次数: |
28067 次 |
| 最近记录: |