2017-02-21 26 views
1

Так ошибка, я получаю:Compiler ошибки при нажатии указателя на объявленный тип вперед в typedef'd вектор

error: no matching function for call to 'std::vector<Type*>::push_back(AnimationAutoComplete::Type*&)' 
neighbours.push_back(samplePoint); 
           ^

Я сделал полосатый вниз версию проекта, чтобы воспроизвести ошибку. Это мой AnimationAutoComplete.h

#include <vector> 

class Type; 
typedef std::vector<Type *> SetOfConstTQ; 

class AnimationAutoComplete { 
public: 
    AnimationAutoComplete() {} 
    ~AnimationAutoComplete() {} 

    SetOfConstTQ getNeighbours(); 

    class Type 
    { 
    public: 
     Type() {} 
     const double point = 3.0; 
    }; 
}; 

И мой main.cpp

#include "AnimationAutoComplete.h" 

SetOfConstTQ AnimationAutoComplete::getNeighbours() 
{ 
    SetOfConstTQ neighbours; 

    Type *samplePoint = new Type(); 

    neighbours.push_back(samplePoint); 

    return neighbours; 
} 

int main() 
{ 
    AnimationAutoComplete main; 
} 
+2

':: type' не' :: Безотносительно :: type'. – LogicStuff

ответ

3

Вы cannot forward declare a nested class.

Определение Type за пределами AnimationAutoComplete и вы исправить вашу программу:

#include <vector> 

class Type; 
typedef std::vector<Type *> SetOfConstTQ; 

class AnimationAutoComplete { ... }; 

class Type { ... }; 
... 

В качестве альтернативы typedef SetOfConstTQ после AnimationAutoComplete::Type полностью определено:

#include <vector> 

struct AnimationAutoComplete { 
    struct Type {}; 
    typedef std::vector<Type*> SetOfConstTQ; 
    SetOfConstTQ getNeighbours(); 
}; 

AnimationAutoComplete::SetOfConstTQ AnimationAutoComplete::getNeighbours() 
{ 
    SetOfConstTQ neighbours; 
    neighbours.push_back(new AnimationAutoComplete::Type()); 
    return neighbours; 
} 

int main() 
{ 
    AnimationAutoComplete main; 
    (void) main.getNeighbours(); 
} 

live on coliru

+0

Следует упомянуть, что перемещение объявления * внутрь * внешнего класса также является допустимым решением. –

+0

@ MaximilianKöstler Я только что сделал это;) – YSC

+0

так что да, ты лучший: D спасибо –

2

Вы не можете объявлять вперед это так. AnimationAutoComplete::Type - это не то же самое, что и Type.

1

Как сказано, вы не можете переслать объявление вложенного класса.

Но если вы хотите, чтобы ваш класс вложенной курица вы можете его в определение типа во классе и использовать позже как:

class AnimationAutoComplete { 
public: 
    AnimationAutoComplete() {} 
    ~AnimationAutoComplete() {} 

    class Type 
    { 
    public: 
     Type() {} 
     const double point = 3.0; 
    }; 

    typedef std::vector<Type *> SetOfConstTQ; // Typedef here 
    SetOfConstTQ getNeighbours(); // use it after typedef 
}; 

AnimationAutoComplete::SetOfConstTQ AnimationAutoComplete::getNeighbours() // return type will come under scope of AnimationAutoComplete 
{ 
    SetOfConstTQ neighbours; 

    Type *samplePoint = new Type(); 

    neighbours.push_back(samplePoint); 

    return neighbours; 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^