【问题标题】:Syntax Error: Identifier (Error C2061)语法错误:标识符(错误 C2061)
【发布时间】:2014-11-22 16:56:37
【问题描述】:

我讨厌发布如此微妙的内容,但这让我完全不知道自己做错了什么: 当我编译时,它根本不喜欢 Class Simulator。我得到了错误

syntax error : identifier 'Simulator'

在我在 DOCO 头文件中使用的每个模拟器实例中。它也为我的 Pellet 结构执行此操作。在我开始在 DOCO.h 中添加与 Simulator 类一起使用的函数之前,该代码运行良好。 Simulator 类使用 DOCO 结构,而 DOCO 结构使用类 Simulator。那是问题吗?也许我使用了错误的包含我的标题?

如果有帮助,这是我收到的错误的链接:http://msdn.microsoft.com/en-us/library/yha416c7.aspx

#include <iostream>
#include <conio.h>
#include <string>
#include "Simulator.h"   //<---Has a chain of includes for other header files
int main()
{
    RandomNumberGen R;
    Simulator S;
    Pellet P;
    DOCO D;

    system("pause");
    return 0;
}

头文件: 模拟器.h

#pragma once
#include <iostream>
#include <stdio.h>
//#include <conio.h>
#include <vector>
#include "Pellet.h"
#include "DataParser.h"
#include "DOCO.h"
#include "RandomNumberGen.h"
#include "Cell.h"
#include "Timer.h"

using namespace std;

class Simulator
{
private:
    int s_iDocoTotal;
    int s_iPelletTotal;
    int s_iGridXComponent;
    int s_iGridYComponent;
    int tempX;
    int tempY;

    //Pellet P;
    //DOCO D;


    static const unsigned int s_iNumOfDir=8;


public:
    Simulator();
    ~Simulator();

    //int GenerateDirection();
    void InitiateDOCO(RandomNumberGen *R, DOCO *D, vector<DOCO>&);  //
    void SpreadFood(RandomNumberGen *R, Pellet *P, vector<Pellet>&, const int x, const int y);      //
    void AddPellet(Pellet *P, RandomNumberGen *R);          //
    void CheckClipping(Pellet *P, RandomNumberGen *R);      //
    void CheckPellets(Pellet *P, RandomNumberGen *R);       //
    void CreateGrid(int x, int y);//
    int GetGridXComponent();    //
    int GetGridYComponent();    //
    int GetDocoTotal();
    vector<DOCO> docoList;                  //Holds the Doco coordinates
    vector<Pellet> pelletList;              //!!Dont use this!! For data import only
    vector<vector<int> > pelletGrid;    //Holds X-Y and pellet count
    char **dataGrid;        //Actual array that shows where units are

    Simulator(const int x, const int y) : 
                s_iGridXComponent(x), 
                s_iGridYComponent(y),
                pelletGrid(x, vector<int>(y)){}
};

DOCO.h

#pragma once
#include <iostream>
#include <stdio.h>
#include <vector>
#include "Simulator.h"
//#include "DataParser.h"



using namespace std;

struct DOCO
{
private:
    int d_iXLocation;
    int d_iYLocation;
    int d_iEnergy;
    int d_iMovement;
    int d_iTemp;
    //Simulator S;
    //RandomNumberGen R;
    //Pellet P;
    enum Direction { NORTH, SOUTH, EAST, WEST, NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST};


public:
    DOCO();
    ~DOCO();
    //int a is the position in docoList to reference DOCO
    int GoNorth(Simulator *S, int a);
    int GoSouth(Simulator *S, int a);
    int GoEast(Simulator *S, int a);
    int GoWest(Simulator *S, int a);
    int GoNorthWest(Simulator *S, int a);
    int GoNorthEast(Simulator *S, int a);
    int GoSouthWest(Simulator *S, int a);
    int GoSouthEast(Simulator *S, int a);

    //int a is the position in docoList to reference DOCO
    void Sniff(Simulator *S, RandomNumberGen *R, int a);        //Detects DOCOs and food
    void Reroute(Simulator *S, RandomNumberGen *R, int a);  //Changes DOCO direction
    void SetDOCO(int tempX, int tempY, int tempEnergy, int tempMovement);
    int GetEnergy();    //
    int SetEnergy();
    int SetMovement();
    int GetMovement();  //
    int GetXLocation(); //
    int GetYLocation(); //
    void SetXLocation(int d_iTemp);
    void SetYLocation(int d_iTemp);
    void EatPellet(Pellet *P, Simulator *S, int a);//ADD DOCO ARGUMENT / DONT OVERLAP DOCO AND PELLETS
    void MoveDoco(Simulator *S, int a);
    void Death();
};

【问题讨论】:

  • 您是否在 main.cpp 中包含这些头文件?
  • 运行和复制很好,你是怎么做到的?
  • 我看到一个没有#includes 的 main() 函数。因此,当您突然引入诸如 Simulator 之类的内容时,编译器当然会抱怨。此外,创建实例时无需重复classstruct 关键字。
  • @Surt:看起来像 &lt;!-- begin snippet: js hide: false --&gt;&lt;!-- end snippet --&gt; 围绕 sn-ps 启用(如果你编辑帖子,你可以看到它们)。
  • 抱歉,忘记在我的#include 中粘贴 main()。帖子已编辑。并且删除了 main() 中的 class 和 struct 关键字,我仍然有同样的错误。谢谢

标签: c++


【解决方案1】:

正如我上面的评论所建议的,您的问题与递归包含有关。

您可以做以下两件事之一:

1) 更改您的 Simulator 类,以便使用对 DOCO 的引用或指向 DOCO 的指针,从而使用forward declare DOCO。

2) 更改 DOCO 标头以使用指向模拟器的指针或对模拟器的引用,然后只需转发声明 Simulator

最简单的更改(如果您的代码中的这些 cmets 仍然存在)是更改 DOCO.h

#pragma once
#include <iostream>
#include <stdio.h>
#include <vector>

class Simulator;

struct DOCO
{
...
};

DOCO 结构定义中,您可以指定对 Simulator 的引用和指针,而不是 Simulator 对象。

我还建议不要using namespace std; 放在头文件中。这在 SO 上的许多线程中都有讨论,为什么不应该在头文件中包含该行。

【讨论】:

    【解决方案2】:

    我确定您不是要转发声明类。

    int main()
    {
    	RandomNumberGen R;
    	Simulator S;
    	Pellet P;
    	DOCO D;
      
        system("pause");
    	return 0;
    }

    【讨论】:

    • 给定一个可见类RandomNumberGen,将类型称为class RandomNumberGen 是完全合法的。问题是(假设这是整个源文件)类名不可见。删除 classstruct 关键字只会更改错误消息。
    • 即使删除 main() 中的关键字结构和类,我也有错误。就可见性而言,其他源文件在使用时都指向结构(或类)。例如:void CheckClipping(Pellet *P, RandomNumberGen *R);
    • @AbductedMonkey 正如我上面的评论所建议的,您的问题与递归包含有关。您需要更改 Simulator 类,以便使用对 DOCO 的引用或指向 DOCO 的指针,从而使用forward declare DOCO。
    • 我刚刚注意到问题中的代码实际上有几个#include 指令。您没有将它们包含在答案的 sn-p 中,我认为这是问题所在。
    【解决方案3】:

    Simulator.h 文件中的#includes 之后,添加该行

    struct DOCO;
    

    在您的Doco.h 文件中的#includes 之后,添加该行

    class Simulator;
    

    这会告诉您的编译器,即使在编译 Simulator 文件时它还不知道 DOCO 是什么,但它最终还是会弄清楚这一点。同样用于编译 DOCO 文件。

    当您像这样前向声明一个类时,在定义该类之前,您无法创建该类的任何实例。但是,您可以创建指向该类的指针或引用。所以如果你的 DOCO 结构需要有一个模拟器,反之亦然,让它们包含指针成员而不是对象成员。

    【讨论】:

      猜你喜欢
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多