[gelöst] nichtssagende Fehlermeldung beim Versuch eine Klasse als Atttribut hinzuzufügen



  • Hallo Leute,

    wir haben in der Uni ein Projekt für das wir in C++/CLI eine Risiko Brettspiel Umsetzung programmieren.
    Nun hänge ich leider an einem komischen Problem und weiß nicht wie ich das ganze lösen kann. Wenn ich versuche in Card.h wie in Zeile 15 eine externe Klasse zu erstellen, kommt immer diese nichtssagende Fehlermeldung (siehe unten).
    Dabei ist es echt egal welche Klasse ich versuche in Card.h als Attribute hinzuzufügen, bei allen das gleiche. Ohne diese eine Zeile 15 kompiliert aber alles ohne Probleme.

    Hat jemand vielleicht eine Ahnung woran das liegt, oder wie ich es rausfinden kann?

    Unten sind alle relevanten Klassendefinitionen, sowie die Fehlercodes, angehängt, sollte noch was fehlen bitte Bescheid geben.

    Viele Grüße

    [cli][b]Card.h[/b]
    
    #pragma once
    
    #include "Country.h"
    
    using System::String;
    
    namespace risc{
        ref class Card
        {
        public: 
            Card(){  };
        private:
            Country ^countries;
    
            int unit;
        };
    }[/cli]
    
    [cli][b]Player.h[/b]
    
    #pragma once
    
    #include "Card.h"
    #include "Country.h"
    
    using System::String;
    using System::Collections::Generic::List;
    
    namespace risc{
        ref class Player
        {
        public:
            Player(String ^name, int color, int typ, int reinforecments)
            {
                _name = name;
                _color = color;
                _typ = typ;
                _reinforcements = reinforecments;
            };
    
            property  String ^name {
                String ^ get() {return _name;}
                void set(String ^newName) {_name = newName;}
            }
            property  int color {
                int get() {return _color;}
                void set(int newColor) {_color = newColor;}
            }
            property int typ {
                int get() {return _typ;}
                void set(int newTyp) {_typ = newTyp;}
            }	
            property int troops {
                int get() {return _reinforcements;}
                void set(int r) {_reinforcements = r;}
            }
    
        private:
            String ^_name;
            int _color;
            int _typ;
            int _reinforcements;
            List<Card^> ^ownedCards;
    
        };
    }[/cli]
    
    [cli][b]Country.h[/b]
    
    #pragma once
    
    #include "Player.h"
    
    using System::String;
    using System::Collections::Generic::List;
    
    namespace risc{
        ref class Country
        {
        public: 
            Country(String ^n, int f, Player ^b, int e){ 
                _name = n;
                _color = f;
                _owner = b;
                _unit = e;
            }
    
            property  String ^name {
                String^ get() {return _name;}
                void set(String^ new_name) {_name = new_name;}
            }
            property  int color {
                int  get() {return _color;}
                void set(int new_color) {_color = new_color;}
            }
    
        private: 
            String ^_name;
            int _color;
            Player ^_owner;
            int _unit;
            List<Country^> ^_frontier;
    
        };
    }[/cli]
    
    [cli][b]Contintent.h[/b]
    
    #pragma once
    
    #include "Country.h"
    
    using System::String;
    using System::Collections::Generic::List;
    
    namespace risc{
        ref class Continent
        {
        public:
            Continent(String^ n, int f, int a) {
                _name = n;
                _color = f;
                _bonus = a;
                _countries = gcnew List<Country^>();
            }
    
            property  String ^name {
                String ^ get() {return _name;}
                void set(String ^new_name) {_name = new_name;}
            }
            property  int color {
                int  get() {return _color;}
                void set(int new_color) {_color = new_color;}
            }
            property int bonus{
                int get(){return _bonus;}
                void set(int new_bonus){_bonus = new_bonus;}
            }
    
        protected:
    
        private:
            String ^_name;
            int _color;
            int _bonus;
            List<Country^> ^_countries;
    
        };
    }[/cli]
    
    [b]Fehlercode[/b] alles in Card.h Line 15 (Linenumber angepasst)
    
    Error	1	error C2143: syntax error : missing ';' before '^'	
    Error	2	error C4430: missing type specifier - int assumed.
    Error	3	error C4430: missing type specifier - int assumed. 
    Error	4	error C2143: syntax error : missing ';' before '^'
    Error	5	error C4430: missing type specifier - int assumed.
    Error	6	error C4430: missing type specifier - int assumed.
    


  • Hallo,

    die Headerdateien verweisen zyklisch aufeinander. Also wirst du nicht ohne Vorwärtsdeklarationen auskommen.

    namespace risc{
    
        ref class Country;
    
        ref class Card
        {
        public: 
            Card(){  };
        private:
            Country ^countries;
    


  • Danke für die schnelle Antwort.

    Ok das ist mir schon aufgefallen, dachte nur nicht das das ein Problem gibt.

    Werde mich mal schlau machen was Vorwärtsdeklaration ist, noch nicht gehört.



  • Der Compiler kann da nicht herausfinden, was ein Country ist ...

    Nehmen wir an, die Übersetzung beginnt bei Card.h ...
    Es wird Country.h includiert ...
    Es wird Player.h includiert ...
    Da wird schon wieder auf Card.h verwiesen und noch immer ist Country unbekannt ...



  • Danke, mal wieder was neues gelernt.

    Also wenn ich jetzt bei Card.h vor ref class Card {...} einfach ein ref class Country hinsetze kompiliert das ganze 🙂

    Ist das so richtig?
    Hab mir das nur kurz zusammen gegoogled, aber macht schon Sinn, da er ja jetzt die Country Klasse vor der Card Klasse aufgerufen wird und er diese somit vorher mal einbinden muss.



  • randooom schrieb:

    die Country Klasse vor der Card Klasse aufgerufen wird

    Eigentlich wurde ihm nur gesagt, dass Country eine Klasse ist und später noch genau spezifiziert wird.

    Der Typ, der da in Card eingefügt wird, ist ja ein Tracking Handle auf Country und kein Country Objekt. Dafür reicht das.


Anmelden zum Antworten