从' complex float转换' to' int'不允许在转换的常数表达式中
我正在使用Protobuf来交换一些消息,但是当我尝试编译使用消息的代码时,我在重复字段中有此转换错误。H文件,特别是在下面的代码中。 是版本问题吗?
.proto文件
message mymessage {
repeated double message = 20;
}
Protobuf重复字段。h
template <int I>
class FastAdderImpl<I, false> {
public:
explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
void Add(const Element& val) { repeated_field_->Add(val); }
private:
RepeatedField* repeated_field_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
};
错误:
/usr/local/include/google/protobuf/repeated_field.h:473:17: error: expected ')'
template <int I>
^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^
/usr/include/complex.h:48:21: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^
/usr/local/include/google/protobuf/repeated_field.h:473:17: note: to match this '('
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^
error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
class FastAdderImpl<I, false> {
^
/usr/local/include/google/protobuf/repeated_field.h:474:23: error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
class FastAdderImpl<I, false> {
^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^~~~~~~~~~
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^~~~~~~~~~~~~~~~~~~~~
2 errors generated.
I am using protobuf to exchange some messages but when i try to compile the code that uses the messages i am having this conversion error in the repeated field.h file, specifically in the code below.
Is it a version problem ?
.proto file
message mymessage {
repeated double message = 20;
}
protobuf repeated field.h
template <int I>
class FastAdderImpl<I, false> {
public:
explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
void Add(const Element& val) { repeated_field_->Add(val); }
private:
RepeatedField* repeated_field_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
};
Error:
/usr/local/include/google/protobuf/repeated_field.h:473:17: error: expected ')'
template <int I>
^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^
/usr/include/complex.h:48:21: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^
/usr/local/include/google/protobuf/repeated_field.h:473:17: note: to match this '('
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^
error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
class FastAdderImpl<I, false> {
^
/usr/local/include/google/protobuf/repeated_field.h:474:23: error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
class FastAdderImpl<I, false> {
^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^~~~~~~~~~
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^~~~~~~~~~~~~~~~~~~~~
2 errors generated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,非类型模板参数必须为编译时间常数,但是非型模板参数的类型(
int
)在您的示例中,与传递参数的类型不同((__ Extension__ 1.0if)
),因此需要转换。也许一个人为的例子可能会进一步解决:
以上会产生类似的错误,说:
demo
The problem is that a non-type template argument must be a compile-time constant but the type of the non-type template parameter(
int
) in your example is not the same as the type of the passed argument((__extension__ 1.0iF)
) and so requires a conversion.Perhaps a contrived example might clear this up further:
The above will produce a similar error saying:
Demo
问题可能是由于某些原因,您将包括
&lt; complect。 h&gt;
c标题。 (或
&lt; ccomplex&gt;
)此标头不应包含在C ++中,但是如果您这样做,则 <代码> i 将是宏。在GCC中,将其定义为
_complex_i
,又定义为(__ Extension__ 1.0if)
;无论哪种方式,很明显它是无效的语法。修复程序是使用与
i
不同的变量名称。(轶事:此错误是在flint中遇到的,最近修复了。)
The problem is likely that for some reason you're including the
<complex.h>
C header. (or<ccomplex>
which is deprecated)This header shouldn't be included in C++, but if you do, then
I
would be a macro. In gcc, it would be defined as_Complex_I
which is in turn defined as(__extension__ 1.0iF)
; either way, it is quite clear that it is invalid syntax.The fix is to use a variable name different from
I
.(anecdote: this bug is encountered in flint, recently fixed.)