2013-02-23 10 views
8

पर परिभाषित किया है, मुझे वास्तव में यह नहीं पता कि इस पुनर्वितरण त्रुटि को कैसे ठीक किया जाए।संरचना त्रुटि की पुनर्वितरण, मैंने इसे केवल

संकलन + ERRORS

g++ main.cpp list.cpp line.cpp 
In file included from list.cpp:5:0: 
line.h:2:8: error: redefinition of âstruct Lineâ 
line.h:2:8: error: previous definition of âstruct Lineâ 

main.cpp

#include <iostream> 
using namespace std; 
#include "list.h" 

int main() { 
    int no; 
    // List list; 

    cout << "List Processor\n==============" << endl; 
    cout << "Enter number of items : "; 
    cin >> no; 

    // list.set(no); 
    // list.display(); 
} 

list.h

#include "line.h" 
#define MAX_LINES 10 
using namespace std; 

struct List{ 
    private: 
     struct Line line[MAX_LINES]; 
    public: 
     void set(int no); 
     void display() const; 
}; 

line.h

#define MAX_CHARS 10 
struct Line { 
    private: 
     int num; 
     char numOfItem[MAX_CHARS + 1]; // the one is null byte 
    public: 
     bool set(int n, const char* str); 
     void display() const; 
}; 

list.cpp

#include <iostream> 
#include <cstring> 
using namespace std; 
#include "list.h" 
#include "line.h" 

void List::set(int no) {} 

void List::display() const {} 

line.cpp

#include <iostream> 
#include <cstring> 
using namespace std; 
#include "line.h" 

bool Line::set(int n, const char* str) {} 

void Line::display() const {} 
+0

संभव dupplicate से समझाया

  • उपयोग करते हैं, गार्ड में शामिल हैं: http://stackoverflow.com/questions/14792903/accessing-classes-from-another-source-in-c-issues-initializing- द कन्स्ट्रक्शन/14792927 # 14792927 –

  • +0

    हेडर गार्ड जोड़ें। –

    उत्तर

    10

    list.cpp में, आप "line.h" और "list.h" दोनों शामिल हैं। लेकिन "list.h" में पहले से ही "line.h" शामिल है, इसलिए "list.h" वास्तव में आपके कोड में दो बार शामिल है। (प्रीप्रोसेसर पर्याप्त स्मार्ट नहीं है जिसमें पहले से कुछ शामिल नहीं है)।

    दो समाधान हैं:

    • "list.h" शामिल न करें अपने list.cpp फ़ाइल में सीधे है, लेकिन यह एक प्रथा है पैमाने पर नहीं करता है: यदि आप अपने हेडर के क्या हर याद फ़ाइल में शामिल है और यह बहुत जल्दी होगा। के रूप में @juanchopanza
    +0

    यही वह था! List.cpp से 'line.h' को हटाया गया है, जिसमें' list.h' 'पहले से ही' line.h' है। धन्यवाद। – eveo

    20

    आप अपने हेडर में include guards डाल करने के लिए की जरूरत है।

    #ifndef LIST_H_ 
    #define LIST_H_ 
    
    // List.h code 
    
    #endif 
    
    +0

    +1। और किसी को कभी भी इसे हर बार करना कभी नहीं भूलना चाहिए) – SChepurin

    +0

    यह सीपीपी पाठ्यक्रम का एक परिचय है। इस तरह कोई कोड इस्तेमाल नहीं कर रहा है, मैं या तो नहीं कर सकता क्योंकि हमने कक्षा में इसे कवर नहीं किया है। क्षमा करें, पुनर्वितरण के लिए एक और आसान समाधान होना चाहिए। – eveo

    +0

    दरअसल, मुझे बस इतना करना था कि मेरी 'list.cpp' में' #include line.h' हटा दें। फिर भी धन्यवाद! – eveo

    1

    आप दो बार "line.h" शामिल हैं, और आपके पास अपनी हेडर फ़ाइलों में गार्ड शामिल नहीं हैं।

    आप की तरह कुछ जोड़ते हैं: अपने हेडर फाइल को

    #ifndef LINE_H 
    #define LINE_H 
    ... rest of header file goes here ... 
    #endif 
    

    , यह सब बाहर से कार्य करेंगे।

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