我认为这不是一个重复的问题.有类似的,但他们没有帮助我解决我的问题.
根据这个,下面是在C++中有效:
class c {
public:
int& i;
};
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我收到以下错误:
error: uninitialized reference member 'c::i'
Run Code Online (Sandbox Code Playgroud)
如何i=0在构造上成功初始化?
非常感谢.
当我尝试编译下面的源代码时出现以下错误.任何人都可以描述为什么会出现此错误以及如何解决此问题?
错误1错误C2758:'A :: s_':必须在构造函数base/member初始化程序中初始化
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A(string& s) : s_(s) { cout << "A::ctor" << endl; }
A(const A& rhs) { cout << "A::copy" << endl; }
~A() { cout << "A::dtor" << endl; }
A& operator=(const A& rhs) { cout << "A::copyassign" << endl; }
private:
string& s_;
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)