2014-11-06 3 views
0

나는 미묘한 것을 게시하는 것을 싫어하지만, 내가 잘못하고있는 것에 완전히 혼란 스럽습니다. 컴파일 할 때 클래스 시뮬레이터를 전혀 좋아하지 않습니다. 오류가 발생합니다구문 오류 : 식별자 (오류 C2061)

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; 
} 

헤더 파일 : Simulator.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(); 
}; 
+0

main.cpp에이 헤더 파일을 포함 하시겠습니까? – radar

+0

실행 및 복사가 잘된 방법, 어떻게 했습니까? – Surt

+0

'# include'가없는 main() 함수가 있습니다. 그래서 컴파일러는 여러분이'Simulator'와 같은 것을 소개 할 때 불평 할 것입니다. 또한 인스턴스를 생성 할 때'class' 및'struct' 키워드를 반복 할 필요가 없습니다. – PaulMcKenzie

답변

0

위의 의견에서 제안한 바와 같이 문제는 재귀 적 포함과 관련되어 있습니다. 따라서 forward declare DOCO 당신이 DOCO에 DOCO 또는 포인터 참조를 사용하도록

1) 귀하의 시뮬레이터 클래스를 변경하고 :

두 가지 중 하나를 수행 할 수 있습니다.

2) Simulator에 대한 포인터 또는 Simulator에 대한 참조를 사용하도록 DOCO 헤더를 변경 한 다음 Simulator으로 선언하십시오.

가장 쉬운 하나 (코드에서 주석이 남아있는 경우) 변경 DOCO.h을 변경하는 것입니다 다음 DOCO 구조체 정의의 내부

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

class Simulator; 

struct DOCO 
{ 
... 
}; 

, 당신은 객체를 시뮬레이터, Simulator에 대한 참조와 포인터를 지정할 수 있습니다.

또한 이 아니며이 아니고 헤더 파일에 using namespace std;을 넣는 것이 좋습니다. 이것은 헤더 파일에 그 행이 없어야하는 이유에 관해서 여기에서 많은 스레드에서 논의됩니다.

+0

'namespace std; '를 헤더에 사용하면 좋은 점이됩니다. – Surt

0

I를 도움이된다면 여기

은 내가 오류에 대한 링크입니다 당신이 수업을 선언 할 뜻이 없다는 것을 확신합니다. 당신의 Simulator.h 파일에 #include를 한 후

+0

보이는 클래스'RandomNumberGen'을 감안할 때, 타입을'class RandomNumberGen'으로 참조하는 것이 합법적입니다. 문제는 (전체 소스 파일이라고 가정하면) 클래스 이름이 보이지 않는다는 것입니다. 'class' 및'struct' 키워드를 제거하면 오류 메시지 만 변경됩니다. –

+0

main()에서 키워드 struct 및 class를 제거하더라도 오류가 있습니다. 가시성에 관한 한, 다른 소스 파일은 struct (또는 클래스)를 가리킬 때 사용됩니다. 예 : void CheckClipping (Pellet * P, RandomNumberGen * R); – AbductedMonkey

+0

@AbductedMonkey 위의 제 의견에서와 같이 귀하의 문제는 재귀 적 포함과 관련이 있습니다. DOCO 또는 DOCO에 대한 참조를 사용하도록 Simulator 클래스를 변경해야하므로'forward declare' DOCO를 사용해야합니다. – PaulMcKenzie

0

int main() 
 
{ 
 
\t RandomNumberGen R; 
 
\t Simulator S; 
 
\t Pellet P; 
 
\t DOCO D; 
 
    
 
    system("pause"); 
 
\t return 0; 
 
}
, 선을, 당신의 Doco.h 파일에 #include를 한 라인

struct DOCO; 

을 추가

class Simulator; 

이것은 당신에게 c 비록 시뮬레이터 파일을 컴파일하는 동안 DOCO가 무엇인지 아직 알지 못한다고해도 결국에는 그 점을 이해할 수 있습니다. 마찬가지로 DOCO 파일을 컴파일합니다.

이와 같이 클래스를 전달 선언하면 클래스가 정의 될 때까지 해당 클래스의 인스턴스를 만들 수 없습니다. 그러나 해당 클래스에 대한 포인터 또는 참조를 만들 수 있습니다. 따라서 DOCO 구조체에 시뮬레이터가 있어야하고 그 반대의 경우에는 객체 멤버가 아닌 포인터 멤버가 포함되도록합니다.

관련 문제