Type unexpected.



  • Hallo,

    Ich habe ein Problem, dem ich einfach nicht auf den Grund komme. Ich habe eine simple header datei, die in meinen augen ok ist, sie will aber nicht compilieren.
    Ich arbeite mit MS Visual Studio .NET 2003, und folgendes ist mein Problem fall:

    #ifndef __ENTITY_H__
    #define __ENTITY_H__
    
    typedef unsigned int IdType;
    
    class Entity {
    protected:
      /** The entity's Id **/
      IdType _id;
    
      /** A C Style String representing the name of the entity **/
      char *_name;
    
    private:
      /**
       * Creates an entity and sets its id and name to the given values.
       **/
      Entity(const IdType id, const char *name const) {
        _id = id;
        _name = new char[strlen(name) + 1];
        strcpy(_name, name);
      }
    
      virtual ~Entity(void) {
        delete _name;
      }
    };
    #endif
    

    Ich bekomme folgende compiler Meldung:
    entity.h(18) : error C2226: syntax error : unexpected type 'IdType'

    Zeile 18 ist der constructor.

    Ich hab schon im netz gesucht und gegoogled aber keine Lösung gefunden. Ich hoffe das es ein simpler Anfänger Fehler ist und ihr mir hier schnell weiterhelfen könnt.

    Danke schnonmal,
    rand



  • Das sieht so aus, als ob da ein paar const's zu viel sind ("IdType id" muß nicht konstant sein, und "char* name const" habe ich noch nie gesehen).

    PS: Im Destruktor solltest du übrigens delete[] verwenden - und einen Copy-Ctor und operator= benötigst du auch noch.



  • Ok, habs hinbekommen.

    zum einen hatte der <string.h> include für strlen gefehlt.
    Zum anderen hattest Du Recht mit dem const char *name const.
    Wenn ich das wegnehme klappts, finde ich allerdings ulkig, weil ich nicht weis
    wieso das nicht gehen sollte.

    Die Bedeutung von const char* ist klar ein konstanter char Pointer, und eigentlich ist const char * const ein konstanter charpointer mit konstantem inhalt.

    Hab ich hier her: http://www.mathematik.uni-marburg.de/~cpp/pointer/const.html
    Daher bin ich überhaupt auf die Idee gekommen sowas zu machen.

    Danke, Du hast mein Tag gerettet 🙂
    rand



  • rand schrieb:

    Die Bedeutung von const char* ist klar ein konstanter char Pointer, und eigentlich ist const char * const ein konstanter charpointer mit konstantem inhalt.

    Ja, aber wenn du dem einen Namen geben willst, heißt das "const char* const name" (achte auf die Reihenfolge!)



  • Ah! Oh!

    2:0 für Dich! 😃

    Gruß,
    rand


Anmelden zum Antworten