简体   繁体   English

C ++错误 - “成员初始化表达式列表被视为复合表达式”

[英]C++ error - “member initializer expression list treated as compound expression”

I'm getting a C++ compiler error which I'm not familiar with. 我收到了一个我不熟悉的C ++编译器错误。 Probably a really stupid mistake, but I can't quite put my finger on it. 可能是一个非常愚蠢的错误,但我不能完全指责它。

Error: 错误:

test.cpp:27: error: member initializer expression list treated as compound expression
test.cpp:27: warning: left-hand operand of comma has no effect
test.cpp:27: error: invalid initialization of reference of type ‘const Bar&’ from expression of type ‘int’

Code: 码:

  1 #include <iostream>
  2
  3 class Foo {
  4 public:
  5         Foo(float f) :
  6                 m_f(f)
  7         {}
  8
  9         float m_f;
 10 };
 11
 12 class Bar {
 13 public:
 14         Bar(const Foo& foo, int i) :
 15                 m_foo(foo),
 16                 m_i(i)
 17         {}
 18
 19         const Foo& m_foo;
 20         int m_i;
 21 };
 22
 23
 24 class Baz {
 25 public:
 26         Baz(const Foo& foo, int a) :
 27                 m_bar(foo, a)
 28         {}
 29
 30         const Bar& m_bar;
 31 };
 32
 33 int main(int argc, char *argv[]) {
 34         Foo a(3.14);
 35         Baz b(a, 5.0);
 36
 37         std::cout << b.m_bar.m_i << " " << b.m_bar.m_foo.m_f << std::endl;
 38
 39         return 0;
 40 }

Note: It looks like the compiler is evaluating the commas in line 27 like here: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/co.htm 注意:看起来编译器正在评估第27行中的逗号,如下所示: http//publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic = / com.ibm.xlcpp8l.doc /语言/ REF / co.htm

edit: Okay, I understand the problem as Alan explained it. 编辑:好的,我理解艾伦解释的问题。 Now, for extra imaginary points, can someone explain how the compiler (g++) came up with the error message it gave? 现在,对于额外的虚构点,有人可以解释编译器(g ++)如何提供它给出的错误消息吗?

m_bar is a reference, so you can't construct one. m_bar是一个引用,因此您无法构造一个。

As others have noted, you can initialise references with the object it refers to, but you can't construct one like you're trying to do. 正如其他人所指出的那样,你可以用引用的对象初始化引用,但是你不能像你想要的那样构造引用。

Change line 30 to 将第30行更改为

const Bar m_bar

and it'll compile / run properly. 并且它将正确编译/运行。

m_bar is declared as a "const reference" and therefore can't be instantiated with the constructor you've supplied. m_bar被声明为“const引用”,因此无法使用您提供的构造函数进行实例化。

Consider making m_bar a member, or passing a pre-constructed Bar object to the constructor. 考虑使m_bar成为成员,或者将预先构造的Bar对象传递给构造函数。

You can see the problem much more clearly in the following code: 您可以在以下代码中更清楚地看到问题:

struct B {
    B( int a, int x  ) {}
};

int main() {
    const B & b( 1, 2);
}

which produces the following errors with g++: 使用g ++产生以下错误:

t.cpp: In function 'int main()':
t.cpp:6: error: initializer expression list treated as compound expression
t.cpp:6: error: invalid initialization of reference of type 'const B&' from expression of type int'

VC++ 6.0 gives the even more gnomic error: VC ++ 6.0提供了更多的gnomic错误:

 error C2059: syntax error : 'constant'

Simply put, you can't initialise references like that. 简单地说,你不能初始化这样的引用。

Although this question is old, for future readers I will point out that the item marked as answer is not correct. 虽然这个问题很老,但对于未来的读者,我会指出标记为答案的项目是不正确的。 A reference can indeed be constructed. 确实可以构建参考。

In an initializer line the code m_bar(foo, a) is trying to use (foo,a) as a constructor for m_bar. 在初始化程序行中,代码m_bar(foo, a)尝试使用(foo,a)作为m_bar的构造函数。 The error tells you that foo will be ignored and you can't construct a Bar out of int a . 该错误告诉您将忽略foo并且您无法构造一个来自int a bar。 The following correct syntax will compile error free: 以下正确的语法将编译无错误:

m_bar (*new Bar(foo,a))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:“初始化表达式列表被视为复合表达式” - error: “initializer expression list treated as compound expression” 错误:初始化表达式列表被视为复合表达式 - error: initializer expression list treated as compound expression 为什么我在下面的 C++ 代码中出现错误:错误:表达式列表在初始化程序中被视为复合表达式 [-fpermissive] - why i am getting error in the below C++ code : error: expression list treated as compound expression in initializer [-fpermissive] 错误:表达式列表在初始化程序[-fpermissive] |中被视为复合表达式 - error: expression list treated as compound expression in initializer [-fpermissive]| 错误:新的初始化表达式列表被视为复合表达式 [-fpermissive] - error: new initializer expression list treated as compound expression [-fpermissive] BST错误:新的初始化程序表达式列表被视为复合表达式 - BST error: new initializer expression list treated as compound expression 表达式列表在初始化程序中被视为复合表达式 - Expression list treated as compound expression in initializer 模板函数中的 C++ 初始化 - 将新的 Initializer 表达式列表视为复合表达式 - C++ initialization in template function - new Initializer expression list treated as compound expression Setenv:类型表达式列表在初始化程序中被视为复合表达式 - Setenv: Type expression list treated as compound expression in initializer 递归调用中出现错误“ [[错误]表达式列表在初始化器[-fpermissive]中被视为复合表达式” - Error “[Error] expression list treated as compound expression in initializer [-fpermissive]” in recursive call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM