最 “vexing parse”问题

语法解析:

C++ 编译器在解析 A b(); 时,优先考虑将其解析为函数声明,而不是对象声明。具体来说:

  • 函数声明A b(); 被解析为声明了一个名为 b 的函数,该函数没有参数,返回类型为 A
  • 对象声明:如果要创建一个 A 类的对象 b,应该使用 A b; 或 A b{};

解决方法

为了避免“最 vexing parse”问题,可以使用以下几种方法来明确表示创建一个对象:

  1. 不带括号:A b;

  2. 使用大括号初始化(C++11 及以后版本):  A b{};

  3. 使用等号初始化:   A b = A();

示例:

#include <iostream>
using namespace std;

class A {
public:
    A() {
        cout << "A constructor called" << endl;
    }
};

int main() {
    // 正确的方式:创建一个 A 类的对象
    A a; // 不带括号
    A b{}; // 使用大括号初始化
    A c = A(); // 使用等号初始化

    // 错误的方式:声明了一个返回类型为 A 的函数
    A d(); // 这实际上是声明了一个函数

    return 0;
}

运行上述代码时,你会看到以下输出:

A constructor called 
A constructor called 
A constructor called

详细解释

  1. A a;

    • 不带括号,明确表示创建一个 A 类的对象 a
    • 调用 A 的默认构造函数。
  2. A b{};

    • 使用大括号初始化,明确表示创建一个 A 类的对象 b
    • 调用 A 的默认构造函数。
  3. A c = A();

    • 使用等号初始化,明确表示创建一个 A 类的对象 c
    • 调用 A 的默认构造函数。
  4. A d();

    • 被解析为声明了一个返回类型为 A 的函数 d,而不是创建一个 A 类的对象。
    • 因此,不会调用 A 的构造函数。


最 vexing parse 问题

A b(); 被解析为声明了一个返回类型为 A 的函数,而不是创建一个 A 类的对象。

  • 解决方法
    • 不带括号:A b;
    • 使用大括号初始化:A b{};
    • 使用等号初始化:A b = A();

/home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:1:22: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B8_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:1:22: note: remove parentheses to default-initialize a variable 1 | bool hang_B8_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:1:22: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B8.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:57, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:1:22: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B9_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:1:22: note: remove parentheses to default-initialize a variable 1 | bool hang_B9_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:1:22: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B9.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:58, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B10_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B10_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B10.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:59, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B11_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B11_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B11.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:60, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B12_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B12_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B12.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:61, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B13_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B13_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B13.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:62, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B14_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B14_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B14.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:63, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:1:23: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 1 | bool hang_B15_zhi_xing() | ^~ /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:1:23: note: remove parentheses to default-initialize a variable 1 | bool hang_B15_zhi_xing() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:1:23: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B15.hpp:2:1: error: a function-definition is not allowed here before ‘{’ token 2 | { | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:16, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/黑匣子.hpp: In member function ‘virtual void hang_B7_zhi_xing()::leiHeiXiaZi_2_16::luo_ji()’: /home/getcrobot/文件_5_9_16.24现在/include/注册/黑匣子.hpp:75:7: error: use of local variable with automatic storage from containing function 75 | FaBuZhe.luo_ji(); | ^~~~~~~ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:15, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/发布/发布者.hpp:112:17: note: ‘hang_B7_zhi_xing()::LeiFaBuZhe_2_8 FaBuZhe’ declared here 112 | LeiFaBuZhe_2_8 FaBuZhe; | ^~~~~~~ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp: In function ‘bool hang_B7_zhi_xing()’: /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:12:1: error: a function-definition is not allowed here before ‘{’ token 12 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:24:1: error: a function-definition is not allowed here before ‘{’ token 24 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:33:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse] 33 | int main() | ^~ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:33:9: note: remove parentheses to default-initialize a variable 33 | int main() | ^~ | -- /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:33:9: note: or replace parentheses with braces to value-initialize a variable /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:34:1: error: a function-definition is not allowed here before ‘{’ token 34 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:42:2: error: expected ‘}’ at end of input 42 | } | ^ In file included from /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/动作注册.hpp:55, from /home/getcrobot/文件_5_9_16.24现在/include/注册/功能注册.hpp:10, from /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:7: /home/getcrobot/文件_5_9_16.24现在/include/注册/动作/行B7.hpp:2:1: note: to match this ‘{’ 2 | { | ^ /home/getcrobot/文件_5_9_16.24现在/main/main.cpp:42:2: warning: control reaches end of non-void function [-Wreturn-type] 42 | } | ^
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值