C++ 成员引用基类型“int”不是结构或联合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21387687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Member Reference base type 'int' is not a structure or union
提问by Alex
I'm running into a Problem in my C++ Code.
我在我的 C++ 代码中遇到了一个问题。
I have a union StateValue
:
我有一个工会StateValue
:
union StateValue
{
int intValue;
std::string value;
};
and a struct StateItem
和一个结构 StateItem
struct StateItem
{
LampState state;
StateValue value;
};
I have a method which goes through a vector of type StateItem
我有一个方法,它通过一个类型的向量 StateItem
for(int i = 0; i < stateItems.size(); i++)
{
StateItem &st = stateItems[i];
switch (st.state)
{
case Effect:
result += std::string(", \"effect\": ") + st.value.value;
break;
case Hue:
result += std::string(", \"hue\": ") + st.value.intValue.str();
break;
case On:
result += std::string(", \"on\": ") + std::string(st.value.value);
break;
default:
break;
}
}
In the case Hue
I get the following Compiler error:
如果Hue
我收到以下编译器错误:
Member reference base type 'int' is not a structure or union
I can′t understand the problem here. Can anyone of you please help me?
我无法理解这里的问题。你们中的任何人都可以帮助我吗?
采纳答案by Mike Seymour
You're trying to call a member function on intValue
, which has type int
. int
isn't a class type, so has no member functions.
你试图调用一个成员函数intValue
,它有型int
。int
不是类类型,因此没有成员函数。
In C++11 or later, there's a handy std::to_string
function to convert int
and other built-in types to std::string
:
在 C++11 或更高版本中,有一个方便的std::to_string
函数可以将int
其他内置类型转换为std::string
:
result += ", \"hue\": " + std::to_string(st.value.intValue);
Historically, you'd have to mess around with string streams:
从历史上看,您必须处理字符串流:
{
std::stringstream ss;
ss << st.value.intValue;
result += ", \"hue\": " + ss.str();
}
回答by Juan Ramirez
Member reference base type 'int' is not a structure or union
Member reference base type 'int' is not a structure or union
int
is a primitive type, it has no methods nor properties.
int
是原始类型,它没有方法或属性。
You are invoking str()
on a member variable of type int
and that's what the compiler is complaining about.
您正在调用str()
类型的成员变量,int
这就是编译器所抱怨的。
Integers cannot be implicitly converted to string, but you can used std::to_string()
in C++11, lexical_cast
from boost
, or the old-slow approach of the stringstream
.
整数不能隐式转换为字符串,但您可以std::to_string()
在 C++11、lexical_cast
fromboost
或stringstream
.
std::string to_string(int i) {
std::stringstream ss;
ss << i;
return ss.str();
}
or
或者
template <
typename T
> std::string to_string_T(T val, const char *fmt ) {
char buff[20]; // enough for int and int64
int len = snprintf(buff, sizeof(buff), fmt, val);
return std::string(buff, len);
}
static inline std::string to_string(int val) {
return to_string_T(val, "%d");
}
And change the line to:
并将该行更改为:
result += std::string(", \"hue\": ") + to_string(st.value.intValue);
回答by scraatz
Your intvalue is no object. It has no member functions. You could use sprintf() or itoa() to convert it to a string.
您的 intvalue 不是对象。它没有成员函数。您可以使用 sprintf() 或 itoa() 将其转换为字符串。
回答by Yves Daoust
intValue
is an int
, it has no methods.
intValue
是一个int
,它没有方法。