C++ 表达式必须具有指向类类型的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16183189/
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
Expression must have pointer-to-class-type
提问by John Kemp
I have a struct "MachineState" and i created a list of type "MachineState*". When i Try to iterate through the list i Keep getting "
我有一个结构体“MachineState”,我创建了一个“MachineState*”类型的列表。当我尝试遍历列表时,我不断得到“
error C2839: invalid return type 'MachineState **' for overloaded 'operator ->
I'm using Microsoft Visual Studio 10. I googled the error and all i could find out was "The -> operator must return a class, struct, or union, or a reference to one."
我使用的是 Microsoft Visual Studio 10。我用谷歌搜索了错误,我能找到的只是“-> 运算符必须返回一个类、结构或联合,或对一个的引用。”
Struct MachineState
{
template <typename MachineTraits>
friend class Machine;
enum Facing { UP, RIGHT, DOWN, LEFT};
MachineState()
: m_ProgramCounter(1)
, m_ActionsTaken(0)
, m_Facing(UP)
, m_Test(false)
, m_Memory(nullptr)
,x(0)
,y(0)
,point1(25, 10)
,point2(10, 40)
,point3(40, 40)
{ }
int m_ProgramCounter;
int m_ActionsTaken;
Facing m_Facing;
bool m_Test;
int x;
int y;
Point point1;
Point point2;
Point point3;
};
I declare the list as
我将列表声明为
std::list<MachineState*> zombs;
Here is where I try to iterate through my list and i keep getting the error, on the "it->point1" saying that the expression must have a pointer to class type.
这是我尝试遍历我的列表的地方,我不断收到错误,在“it->point1”上说表达式必须有一个指向类类型的指针。
for(std::list<MachineState*>::iterator it = zombs.begin(); it != zombs.end(); it++)
{
Point points[3] = {it->point1, it->point2, it->point3};
Point* pPoints = points;
SolidBrush brush(Color(255, 255, 0, 0));
m_GraphicsImage.FillPolygon(&brush, pPoints,3);
}
If anyone can explain me what's wron
如果有人能解释我有什么问题
回答by Drew Dormann
it
is an iterator to a pointerto a MachineState
.
it
是一个迭代一个指针到一个MachineState
。
You need to dereference the iterator and thenthe pointer.
您需要取消引用迭代器,然后取消引用指针。
Point points[3] = {(*it)->point1, (*it)->point2, (*it)->point3};
Edit:
编辑:
Dereferencingmeans getting the thing that it's referring to.
取消引用意味着获得它所指的东西。
Dereferencing is done with the *
or ->
operator.
取消引用是通过*
or->
操作符完成的。
If it
were a MachineState
, you could use it.point1
如果it
是 a MachineState
,你可以使用it.point1
If it
were a pointerto a MachineState
, you could use it->point1
or (*it).point1
如果it
是一个指针到MachineState
,你可以使用it->point1
或(*it).point1
If it
were a iteratorto a MachineState
, you could also use it->point1
or (*it).point1
如果it
是一个迭代的MachineState
,你也可以使用it->point1
或(*it).point1
Since it
is an iterator to a pointerto a MachineState
, you must use (*it)->point1
or (**it).point1
由于it
是一个迭代器的指针到MachineState
,你必须使用(*it)->point1
或(**it).point1