相关疑难解决方法(0)

如何使用C++中的默认构造函数定义和声明变量?

根据我对声明和定义的理解,在全球范围内:

MyClass instance();//Declares a function that returns a MyClass
MyClass instance;//Declares an instance of MyClass
Run Code Online (Sandbox Code Playgroud)

是否可以声明一个变量并将其定义为在全局范围内使用默认构造函数?如果我使用结构而不是类,该怎么办?

编辑:

好的,MyClass instance;调用默认构造函数也是如此.任何人都可以解释这与这个例子的一致性:

int a; // not default constructed, will have random data 
int b = int(); // will be initialised to zero
Run Code Online (Sandbox Code Playgroud)

c++ constructor default-constructor

3
推荐指数
1
解决办法
1367
查看次数

`(&)()`在C++编译器错误消息中是什么意思

这个错误是什么意思?

error: no matching function for call to `foo::bar(Qux (&)())`
Run Code Online (Sandbox Code Playgroud)

我知道编译器(g ++)无法将函数调用与foo成员函数匹配.我特意询问最后的额外内容; 这一点:(&)().

作为参考,我的函数调用是这样的:

// inside another member function of `foo`
Qux qux();
this->bar(qux);
Run Code Online (Sandbox Code Playgroud)

签名bar是:

virtual void bar(Qux&);
Run Code Online (Sandbox Code Playgroud)

编译器还说唯一的候选者是:

virtual void bar(Qux&);
Run Code Online (Sandbox Code Playgroud)

我的函数调用签名与我的定义签名有何不同,具有什么(&)()意义?

c++ g++

3
推荐指数
1
解决办法
174
查看次数

标签 统计

c++ ×2

constructor ×1

default-constructor ×1

g++ ×1