小编kam*_*ilm的帖子

使用参数调用类构造函数 - 'x'中非成员类型的成员的请求

我有一个类A接受B类作为构造函数参数.B类可以用int值构造.我原来的代码非常复杂,但我希望我把它简化为基本情况:

class B {
public:
    explicit B(int a) : val(a) {}
private:
    int val;
};

class A {
public:
    A(const B & val) : value(val) {};
    void print() {
        //does nothing
    }

private:
    B value;
};


int main() {
    int someTimeVar = 22;
    A a(B(someTimeVar));
    a.print();
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误代码:

$ g++ test.cpp -Wall -O0
test.cpp: In function ‘int main()’:
test.cpp:22:7: error: request for member ‘print’ in ‘a’, which is of non-class type ‘A(B)’
     a.print();
       ^
test.cpp:20:9: warning: unused variable ‘someTimeVar’ [-Wunused-variable] …
Run Code Online (Sandbox Code Playgroud)

c++ gcc most-vexing-parse

4
推荐指数
2
解决办法
207
查看次数

标签 统计

c++ ×1

gcc ×1

most-vexing-parse ×1