typedef VOR Deklaration?



  • Hi,

    Wieso kompiliert das hier?

    typedef shared_ptr<MyClass> MyClassPtr;
    
    class MyClass {
    
    };
    

    An der Stelle mit dem typedef ist MyClass ja noch nicht mal deklariert. Ist das gültiger Code? Wieso geht das?



  • Hä? Bei mir funzt garnix.

    #include "boost/shared_ptr.hpp"
    
    typedef boost::shared_ptr<MyClass> MyClassPtr;
    
    class MyClass {
    
    };
    

    C(++) Saves\TEMPS\TEMP.cxx|3|error: 'MyClass' was not declared in this scope|
    C(++) Saves\TEMPS\TEMP.cxx|3|error: template argument 1 is invalid|
    C(++) Saves\TEMPS\TEMP.cxx|3|error: invalid type in declaration before ';' token|

    GCC 4.7



  • GCC:

    ./test.cpp:3:25: Fehler: »MyClass« wurde in diesem Gültigkeitsbereich nicht definiert
    ./test.cpp:3:32: Fehler: Templateargument 1 ist ungültig
    ./test.cpp:3:44: Fehler: invalid type in declaration before »;« token
    


  • Ehrlich gesagt würde es mich wundern, wenn das kompilieren soll...

    Du brauchst zumindest eine Vorwärtsdeklaration von MyClass;

    Aber dann funktioniert es, weil in dem shared_ptr nur Zeiger (und Referenzen) auf das Objekt
    verwendet werden.

    Wie du siehst, brauchst du hier auch nicht die kompletten Objekt-Informationen:

    class MyClass;
    
    template<typename T>
    class my_shared_ptr{
    public:
    	my_shared_ptr(T* ptr) : ptr(ptr){
    	}
    private:
    	T* ptr;
    };
    
    class MyClass { 
    
    };
    

    Gruß,
    Xspille



  • @pyhax: Was is'n das für ein Compiler, halb englisch halb deutsch? 😃



  • Hacker schrieb:

    @pyhax: Was is'n das für ein Compiler, halb englisch halb deutsch? 😃

    Ich kann es auch english machen 😃 :

    [pyhax@archlinux tmp]$ LANG="en_US" g++ ./test.cpp -otest -g -std=c++0x -Wall -Wextra -pedantic -Weffc++
    ./test.cpp:3:25: error: `MyClass' was not declared in this scope
    ./test.cpp:3:32: error: template argument 1 is invalid
    ./test.cpp:3:44: error: invalid type in declaration before `;' token
    

Anmelden zum Antworten