错误:“预期类型说明符”[关闭]

问题描述 投票:0回答:1

我收到此错误,我不知道为什么在尝试创建新的对象指针时。

这是我的标题类的代码

#ifndef CURSOR_H_INCLUDED
#define CURSOR_H_INCLUDED
#include <SFML\Graphics.hpp>
#Include "Mango.h"
#include <stack>

using namespace sf;
using namespace std;

struct cursor{
 Texture tCursor;
 Sprite sCursor;
 stack<Mango*> inv;
 float money;
 void Sell();
 cursor();
 ~cursor();
};

#endif CURSOR_H_INCLUDED

并且主要是尝试这样做

cursor * cursor = new cursor();

但它给了我这个错误。

c++ struct sfml
1个回答
3
投票

您已将指针命名为与类名相同的名称。你做不到:

struct Foo {int a;};

int main()
{
        Foo* Foo = new Foo(); // Because
// After here ^^^ Foo is no longer a type but a variable. And you can't "new"
// a variable. Thanks to user4581301 for teaching me this.

    return 0;
}

在:

cursor * cursor = new cursor();
         ^^^^^^

将指针名称从光标更改为其他名称。

附:有趣的是,感谢user4581301我知道变量CAN与用户定义的类型具有相同的名称,但显然是个坏主意。所以:

Foo Foo; // Fine
Foo.a = 7; // Fine
Foo newFooObj; // Doesn't make sense, Foo is no longer seen as a type 
© www.soinside.com 2019 - 2024. All rights reserved.