相关疑难解决方法(0)

在构造函数C++中初始化引用

我认为这不是一个重复的问题.有类似的,但他们没有帮助我解决我的问题.

根据这个,下面是在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在构造上成功初始化?

非常感谢.

c++ constructor

27
推荐指数
3
解决办法
5万
查看次数

必须在构造函数base/member初始化程序中初始化带错误的引用变量

当我尝试编译下面的源代码时出现以下错误.任何人都可以描述为什么会出现此错误以及如何解决此问题?

错误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)

c++ constructor compiler-errors visual-studio-2012

5
推荐指数
1
解决办法
2万
查看次数