views:

57

answers:

3

Hello, everyone,

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;
manga3->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.

+4  A: 

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.,

Alf P. Steinbach
This time you were the first to answer. `:-)`
Prasoon Saurav
I got lucky. I think maybe I'll do what real programmers do: automate the thing. Of course, then I first need to create an expert system for synthesizing answers. ;-)
Alf P. Steinbach
+1  A: 

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.

awoodland
A: 

Try:

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