开发者

C++ - Undefined reference issues when working with classes

I am working on a small project where I use multiple classes. One of those classes is Menu, which has a showContainer method. Here's the class declaration:

class Menu {
    //snip
    Menu();
    Menu(std::string, std::string, int, int);
    virtual ~Menu();
    //snip
    /**
     * Visualiza e providencia navegacao presente num container
     * @param Container a mostrar
     * @return Indice seleccionado pelo utilizador
     */
    template <class C>
    void showContainer(std::list<C>, int, int);
};

It compiles fine. I added the following test to the project's main.cpp:

Menu menu;
Manga* manga1;
manga1->setCapacidade(60);
manga1->setCategoria(LongoCurso);
manga1->setLocalizacao("Norte");
manga1->setNumero(143);
Manga* manga2;
manga2->setCapacidade(60);
manga2->setCategoria(LongoCurso);
manga2->setLocalizacao("Norte");
manga2->setNumero(143);
Manga* manga3;
m开发者_StackOverflow中文版anga3->setCapacidade(60);
manga3->setCategoria(LongoCurso);
manga3->setLocalizacao("Norte");
manga3->setNumero(143);

std::list<Manga *> teste;
teste.push_back(manga1);
teste.push_back(manga2);
teste.push_back(manga3);
menu.showContainer(teste, 5, 0);

return 0;

This returns the following compiler error:

C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src/main.cpp:96: undefined reference to `void Menu::showContainer<Manga*>(std::list<Manga*, std::allocator<Manga*> >, int, int)'

Any guesses?

Thanks for your time.


You definition of the Menu::showContainer function template must be visible to the code calling it, unless it's been explicitly instantiated for the type used in the call.

Chances are you've defined down in some implementation file.

If so, move it into the header.

Cheers & hth.,


Looks like the compiler is only seeing a declaration of showContainer, not a definition when it's compiling main.cpp. See C++FAQLite article on this one.


Try:

menu.showContainer<Manga*>(teste, 5, 0); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜