2011-06-29 7 views
7

तो, मैं उम्मीद करता हूं कि यह एक साधारण उत्तर बनने की उम्मीद है, लेकिन मैं थोड़ी देर के लिए इसमें हैकिंग कर रहा हूं, और ऐसा नहीं लगता इस मुद्दे को ठीक करने के लिए। , अबउपयोगकर्ता द्वारा निर्मित शीर्षलेख C2061 का कारण बनना: सिंटेक्स त्रुटि: पहचानकर्ता 'वर्गनाम'

#ifndef INTERSECTION_H 
#define INTERSECTION_H 

#include "Coord.h" 
#include "Road.h" 
#include "TrafficLight.h" 

class Intersection { 
private: 
    int id; 
    Coord * midPoint; 
    Road * northRoad; 
    Road * eastRoad; 
    Road * westRoad; 
    Road * southRoad; 
    TrafficLight * trafficLight; 
public: 
    Intersection(int, Coord *, Road *, Road *, Road *, Road *); 
    ~Intersection(); 
    void transitionTrafficLight(); 
    int getId(); 
    Road * getNorthRoad(); 
    Road * getEastRoad(); 
    Road * getWestRoad(); 
    Road * getSouthRoad(); 
    TrafficLight * getTrafficLight(); 
}; 

#endif 

अगर मैं इस वर्ग के उपयोग करने का प्रयास:

error C2061: syntax error : identifier 'Intersection'

यह मेरा चौराहे हैडर है: तो मैं एक विशेष वर्ग, Intersection, कि जब किसी अन्य शीर्ष में शामिल मुझे देता है कहीं और, मुझे त्रुटि मिलती है। उदाहरण के लिए:

#ifndef ROAD_H 
#define ROAD_H 

#include "Coord.h" 
#include "Intersection.h" 
#include <string> 

class Road { 

public: 
    enum LaneCount { TWO_LANE = 2, FOUR_LANE = 4 }; 
    Road(std::string, Coord *, Coord *, LaneCount, Intersection *, Intersection *, int); 
//shortened 

विशेष रूप से (संदर्भ और किसी भी अन्य वर्ग है जो Intersection) Road निर्माता पर। मुझे नहीं लगता कि यह एक वाक्यविन्यास समस्या है, क्योंकि Coord एक और वर्ग है, जिसे उसी तरीके से परिभाषित किया गया है, और संकलक (वीएस 2008) इसके बारे में शिकायत नहीं करता है। यह विशेष रूप से Intersection है जो मुझे यह समस्या दे रहा है। :/

मैं इसे होमवर्क टैग कर रहा हूं - यह वही है, भले ही यह केवल एक त्रुटि है, मैं समस्या के भाग के बजाय छुटकारा नहीं पा सकता हूं।

विचार?

+2

ऐसा लगता है कि आपको ROAD_H फ़ाइल में 'कक्षा छेड़छाड़' घोषित करने की आवश्यकता है। – GWW

+0

दिलचस्प। ऐसा लगता है कि ऐसा किया है। क्या आप समझा सकते हैं कि पूरे हेडर को शामिल करते समय यह क्यों काम करता है, और क्यों केवल उस वर्ग में कोई समस्या नहीं होगी? इसे एक जवाब में करें और मैं खुशी से आपको चेक दूंगा। :) – kcoppock

उत्तर

25

ऐसा लगता है कि आपके पास दो शीर्षलेख फ़ाइलें हैं जो गोलाकार रूप से एक-दूसरे सहित हैं - intersection.h और road.h. ऐसा करने से गार्ड्स के काम को शामिल करने के कारण सी ++ में अजीब आश्चर्य हो जाती है। उदाहरण के लिए, मान लीजिए मैं दो हेडर फाइल है कि इस तरह लग रहे है: अब

// File: A.h 
#ifndef A_Included 
#define A_Included 

#include "B.h" 

class A {}; 

void MyFunction(B argument); 
#endif 

और

// File: B.h 
#ifndef B_Included 
#define B_Included 

#include "A.h" 

class B {}; 

void MyOtherFunction(A argument); 
#endif 

, अगर मैं #include "A.h" करने की कोशिश, तो यह

// File: A.h 
#ifndef A_Included 
#define A_Included 

#include "B.h" 

class A {}; 

void MyFunction(B argument); 
#endif 

के लिए बाहर का विस्तार जब मैं #include "B.h" का विस्तार करने का प्रयास करता हूं, तो मुझे यह मिलता है:

// File: A.h 
#ifndef A_Included 
#define A_Included 

// File: B.h 
#ifndef B_Included 
#define B_Included 

#include "A.h" 

class B {}; 

void MyOtherFunction(A argument); 
#endif 

class A {}; 

void MyFunction(B argument); 
#endif 
अब

// File: A.h 
#ifndef A_Included 
#define A_Included 

// File: B.h 
#ifndef B_Included 
#define B_Included 

// File: A.h 
#ifndef A_Included 
#define A_Included 

#include "B.h" 

class A {}; 

void MyFunction(B argument); 
#endif 

class B {}; 

void MyOtherFunction(A argument); 
#endif 

class A {}; 

void MyFunction(B argument); 
#endif 

, देखते हैं क्या होता है जब हम हल इन अजीब के सभी गार्ड शामिल कर सकते हैं:

इस बिंदु पर, पूर्वप्रक्रमक फिर A.h बाहर का विस्तार, की कोशिश करेंगे जो इस ओर जाता है। पहली बार जब हम ए देखते हैं, तो यह विस्तारित होता है, जैसा कि हम पहली बार बी का विस्तार करते हैं। हालांकि, जब हम दूसरी बार ए देखते हैं, तो यह बिल्कुल विस्तारित नहीं होता है।

class B {}; 
void MyOtherFunction(A argument); 
class A {}; 
void MyFunction(B argument); 

सूचना है कि जब MyOtherFunction घोषित किया जाता है, A अभी तक घोषित नहीं किया गया है, और इसलिए संकलक एक त्रुटि रिपोर्ट: इस प्रकार, टिप्पणियों और पूर्वप्रक्रमक निर्देशों बाहर लेने के बाद, हम इस परिणामी कोड मिलता है।

इसे ठीक करने के लिए आपको आगे-घोषणा कर सकते हैं A और हेडर फाइल है कि उन्हें जरूरत है B:

// File: A.h 
#ifndef A_Included 
#define A_Included 

class A {}; 
class B; // Forward declaration 

void MyFunction(B argument); 
#endif 

और

// File: B.h 
#ifndef B_Included 
#define B_Included 

class B {}; 
class A; // Forward declaration 

void MyFunction(B argument); 
#endif 

अब, वहाँ कोई और अधिक परिपत्र निर्भरता है। जब तक आप #include .cpp फ़ाइलों में उचित शीर्षलेख फ़ाइलें, तब तक आपको ठीक होना चाहिए।

आशा है कि इससे मदद मिलती है!

+0

अरे! यह भी एक अच्छा जवाब है; इस पर गहराई से जाने के लिए धन्यवाद। मैं सी ++ (जावा पर ध्यान केंद्रित कर रहा हूं) पर अविश्वसनीय रूप से जंगली हूं, इसलिए यह एक अच्छा रिफ्रेशर है। धन्यवाद! – kcoppock

+0

आपने, मेरा दिन बचा लिया है! –

संबंधित मुद्दे