2013-04-14 11 views
5

के लिए अनिर्धारित संदर्भ संपादित करें: नीचे दिए गए निश्चित कोड को पोस्ट करें। आपकी मदद के लिए सभी को शुक्रिया!सी ++ वर्ग विरासत, 'कक्षा :: कन्स्ट्रक्टर()'

मैं सिर्फ सी ++ सीख रहा हूं और विरासत में परेशानी हो रही हूं। मैंने खोजा और खोजा है और कुछ भी करने की कोशिश की है लेकिन मैं इस कोड को संकलित करने के लिए संकलित नहीं कर सकता हूं, जो कार्यक्षमता को बनाए रखना है।

मुझे लगता है कि मैं एक बेवकूफ गलती कर रहा हूं या शायद मुझे कुछ बड़ी अवधारणा याद आ रही है, लेकिन अगर कोई इसे देख सके तो मैं वास्तव में इसकी सराहना करता हूं!

यदि मैं ऑब्जेक्ट बनाने वाले स्टारसिस्टम कन्स्ट्रक्टर में 3 लाइनों पर टिप्पणी करता हूं, तो यह संकलित करता है, इसलिए मुझे पता है कि इस मुद्दे को करना है।

#include <iostream> 
    #include <vector> 
    #include <string> 
    #include <stdlib.h> 
    #include <time.h> 

    using namespace std; 

    class SystemBody 
    { 
     public: 
      SystemBody(); 
      int systembodyindex; 
      int starsystemindex; 

      SystemBody(int systembodyindex, int starsystemindex) 
      { 
       cout << "StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl; 
      } 
    }; 


    class Star : public SystemBody 
    { 
     public: 
      Star(); 
      string startype; 

      Star(int systembodyindex, int starsystemindex) 
      { 
       cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl; 
      } 
    }; 

    class Planet : public SystemBody 
    { 
     public: 
      Planet(); 
      string planettype; 

      Planet(int systembodyindex, int starsystemindex) 
      { 
       cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl; 
      } 

    }; 

    class ExitNode : public SystemBody 
    { 
     public: 
      ExitNode(); 
      vector<int> connectedindexlist; 
      ExitNode(int systembodyindex, int starsystemindex) 
      { 
       cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Exit Node " << systembodyindex << endl; 
      } 


    }; 


    class StarSystem 
    { 
     public: 
      StarSystem(); 
      int starsystemindex; 
      vector<StarSystem> connectedlist; 
      vector<Planet> planetlist; 

      StarSystem(int index) 
      { 
       starsystemindex = index; 
       cout << "--Creating StarSystem: " << starsystemindex << endl; 
       int numberofbodies = (rand() % 4) + 2; 
        for (int i = 0; i < numberofbodies; i +=1) 
        { 
         if (i == 0) 
         { 
          Star body(i, starsystemindex); 
         } 
         else if (i == numberofbodies) 
         { 
          ExitNode body(i, starsystemindex); 
         } 
         else 
         { 
          Planet body(i, starsystemindex); 
         } 

        } 

      } 

      void addConnection(StarSystem connectedstarsystem) 
      { 
       cout << "--StarSystem " << starsystemindex << ": Adding connection to StarSystem " << connectedstarsystem.starsystemindex << endl; 
       connectedlist.push_back(connectedstarsystem); 
      } 

    }; 



    int main() 
    { 
     srand(time(0)); 
     StarSystem starsystem0(0); 
     return 0; 
    } 

संपादित करें:

आपकी मदद के लिए हर किसी के लिए धन्यवाद! अगर भविष्य में किसी भी को यह उपयोगी लगे तो बस यहां निश्चित कोड पोस्ट कर रहा है।

#include <iostream> 
#include <vector> 
#include <string> 
#include <stdlib.h> 
#include <time.h> 

using namespace std; 

class SystemBody 
{ 
    public: 
     int systembodyindex; 
     int starsystemindex; 
     SystemBody () 
     { 
      cout << "----SystemBody BEING CREATED WITH NO PARAMETERS" << endl; 
     } 
     SystemBody (int bodyindex, int systemindex) 
     { 
      systembodyindex = bodyindex; 
      starsystemindex = systemindex; 
      cout << "----StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl; 
     } 

}; 


class Star : public SystemBody 
{ 
    public: 
     Star (int bodyindex, int systemindex) : SystemBody (bodyindex, systemindex) 
     { 
      cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl; 
     } 
}; 


class Planet : public SystemBody 
{ 
    public: 
     Planet (int bodyindex, int systemindex) : SystemBody (bodyindex, systemindex) 
     { 
      cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl; 
     } 
}; 

class ExitNode : public SystemBody 
{ 
    public: 
     ExitNode (int bodyindex, int systemindex) : SystemBody (bodyindex, systemindex) 
     { 
      cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into ExitNode " << systembodyindex << endl; 
     } 
}; 


class StarSystem 
{ 
    public: 
     int starsystemindex; 
     vector<StarSystem> connectedlist; 
     vector<Planet> planetlist; 

     StarSystem (int index) 
     { 
      starsystemindex = index; 
      cout << "--Creating StarSystem: " << starsystemindex << endl; 
      int numberofbodies = (rand() % 4) + 2; 
      for (int i = 0; i <= numberofbodies; i +=1) 
      { 
       if (i == 0) 
       { 
        Star body(i, starsystemindex); 
       } 
       else if (i == numberofbodies) 
       { 
        ExitNode body(i, starsystemindex); 
       } 
       else 
       { 
        Planet body(i, starsystemindex); 
       } 
      } 
     } 
}; 

int main() 
{ 

    srand(time(0)); 
    StarSystem starsystem0(0); 
    return 0; 
} 
+0

क्या आप यह निर्दिष्ट करना चाहते हैं कि आप कौन सी कार्यक्षमता चाहते हैं? – jepugs

+0

मैं एक जेनेरिक सिस्टमबॉडी क्लास रखने की कोशिश कर रहा हूं, जिसमें सदस्य गुण और विधियां होंगी जो वंचित वर्ग स्टार, ग्रह और निकास के लिए आम हैं। – phimath

+0

कन्स्ट्रक्टर में आप 'स्टार', 'ExitNode' और' Planet' ऑब्जेक्ट्स को स्टैक-आधारित स्वचालित चर के रूप में प्रारंभ कर रहे हैं - जैसे ही वे दायरे से बाहर निकल जाएंगे, इन सभी को हटा दिया जाएगा। क्या आप इन्हें किसी सूची या वेक्टर में जोड़ने के लिए हैं? –

उत्तर

9

शायद यह सिर्फ मुझे है, लेकिन यहां अपने निर्माता एक सरल declration है कि परिभाषित नहीं किया गया है:

class StarSystem 
    { 
     public: 
      StarSystem(); // <--- Undefined! 

आप एक निर्माता घोषणा की है, लेकिन वहाँ क्या वास्तव में इस निर्माता में हो रहा है की कोई परिभाषा है ।

यदि यह एक निर्माता है कि बस कुछ नहीं करता है,

StarSystem() {} // Defined. 
// Nothing happens inside, but everything gets default-constructed! 

कर एक तरफ ध्यान दें, जब चीजें इस प्रकार की पोस्टिंग, यह त्रुटि संख्या पोस्ट और एक टिप्पणी या का सूचक किसी तरह डाल करने के लिए मदद करता है के रूप में जहां त्रुटि हो रही है (इसलिए हम इसे कोड के अपने विशाल ब्लॉब में देख सकते हैं)।

संपादित करें: एक महत्वपूर्ण नोट के रूप में, यदि आप उस निर्माता का उपयोग नहीं कर रहे हैं, तो बस इसे हटाएं।

+0

धन्यवाद, क्या यह उचित परिभाषा नहीं है? स्टारसिस्टम (इंटेल इंडेक्स) {....}? – phimath

+0

@zacharydimaria नहीं, यह एक उचित परिभाषा है। आपके पास अभी 2 रचनाकार हैं, (या कम से कम, सी ++ आपको लगता है) क्योंकि आपके पास 'स्टारसिस्टम(); शीर्ष पर तैर रहा है। यदि आप नहीं चाहते कि वह एक कन्स्ट्रक्टर बन जाए, तो इसे पूरी तरह से कोड से हटा दें। –

1

आपने कई डिफ़ॉल्ट रचनाकारों को परिभाषित किया लेकिन उन्हें लागू नहीं किया। इसके बजाय

StarSystem(); // <- it is OK if you implement this somewhere but you didn't 

लिखने

StarSystem(){} 
      ^^ this is empty implementation 
3

आपको लगता है कि क्योंकि आपको बुला रहा नहीं कर रहे हैं SystemBody() आप इसे परिभाषित करने की जरूरत नहीं है की। हालांकि आप इसे अप्रत्यक्ष रूप से बुला रहे हैं।

और do't के रूप में सुझाव

SystemBody() {}; 

है। यह वही नहीं है जो आप चाहते हैं। यदि आप इसका उपयोग नहीं करते हैं तो इसे पूरी तरह से हटा दें।


आपका वर्ग स्टार SystemBody से विरासत। इसका मतलब है कि जब एक नया Star बनाया गया है तो SystemBody के लिए कन्स्ट्रक्टर कहा जाता है।

इस लाइन

Star(int systembodyindex, int starsystemindex) 
    { 

वास्तव में

Star(int systembodyindex, int starsystemindex) : 
    SystemBody() // Here 
    { 

को संकलित संकलक SystemBody के लिए डिफ़ॉल्ट निर्माता कहता है कि यदि आप एक अपने आप को कॉल नहीं करते है।


आप इसके बारे में सोचते हैं, तो आप किसी भी तरह SystemBody प्रारंभ करने में जब आप एक नया Star बनाने की जरूरत है। आप इसे स्पष्ट रूप से

Star(int systembodyindex, int starsystemindex) : 
    SystemBody(systembodyindex) // Here 
    { 
+0

मैंने सोचा कि मेरा क्लास स्टार सिस्टमबॉडी विरासत में था, स्टारसिस्टम नहीं? – phimath

+0

@zacharydimaria ओह मेरे बुरे। मैं इसे संपादित करेंगे। लेकिन स्थिति एक ही है। – stardust

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