“无效转换”条件运算符错误
我在这一行中收到编译错误:
cout << (MenuItems[i].Checkbox ? (MenuItems[i].Value ? txt::mn_yes : txt::mn_no) : MenuItems[i].Value)
错误:
menu.cpp|68|error: invalid conversion from 'int' to 'const char*'
menu.cpp|68|error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
MenuItems 是以下类的 std::vector:
class CMenuItem
{
public:
string Name;
int Value;
int MinValue, MaxValue;
bool Checkbox;
CMenuItem (string, int, int, int);
CMenuItem (string, bool);
};
mn_yes 和 mn_no 是 std::strings。
编译器是 MinGW(与 code::blocks 一起分发的版本)。
I'm getting compile error in this line:
cout << (MenuItems[i].Checkbox ? (MenuItems[i].Value ? txt::mn_yes : txt::mn_no) : MenuItems[i].Value)
Error:
menu.cpp|68|error: invalid conversion from 'int' to 'const char*'
menu.cpp|68|error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
MenuItems is std::vector of following class:
class CMenuItem
{
public:
string Name;
int Value;
int MinValue, MaxValue;
bool Checkbox;
CMenuItem (string, int, int, int);
CMenuItem (string, bool);
};
mn_yes and mn_no are std::strings.
Compiler is MinGW (version that is distributed with code::blocks).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个可能的条件值必须可转换为通用类型。问题是外部条件: 的左边
始终是一个
string
,但右边的:是一个int。它试图通过
const char *
->string
找到一种方法,但随后它不允许int
到const char *
转换(这很好,因为它毫无意义)。只需执行:或类似操作。
The two possible conditional values have to be convertible to a common type. The problem is that the left of the outer conditional:
is always a
string
, but the right:is an int. It tries to find a way by going
const char *
->string
, but then it won't allow theint
toconst char *
conversion (which is good, because it would be meaningless). Just do:or similar.