c++ - g++ duplicate symbol error when working with templates (noob question) -


so i'm trying pick c++, , decided write generic group class using templates, takes type , size template parameters:

in group.h:

#ifndef __group_h #define __group_h #define max_size 10  /**********************************************************  * define group class handles collection of members  * of type  **********************************************************/ template <class type, int max> class group {   private:       std::string name;       int count, size;       type * members[max];    public:       group();       group(const std::string & _name);       ~group();        // display instance info       virtual void show();        // add member       void add_member(type &);        // list memebers       void list();        // name setter/getter       void set_name(const std::string &);       const std::string & get_name();  };  #endif 

group.cc:

/**********************************************************  * class methods group  **********************************************************/ template <class type, int max> group<type, max>::group() : count(0), size(max), name("new group") {};  template <class type, int max> group<type, max>::group(const std::string & _name) : name(_name), count(0), size(max) {};  template <class type, int max> group<type, max>::~group() {   int = 0;    while(i < this->count) {     delete this->members[i];     ++i;   } }  template <class type, int max> void group<type, max>::show() {     std::cout << "<#group - name: " << this->name << ", members: " << this->count << "/" << this->size << " >\n"; }  template <class type, int max> void group<type, max>::add_member(type & member) {     if (this->count < this->size) {         this->members[this->count] = &member;         this->count++;     } else {         std::cout << "error - group full!\n";     } }  template <class type, int max> void group<type, max>::list() {     int = 0;     std::cout << "the following members of group " << this->name <<":\n";     // assumes member has show() method implemented     while (i < this->count) {         std::cout << << ". ";         (this->members[i])->show();         ++i;     } }  template <class type, int max> void group<type, max>::set_name(const std::string & _name) {     this->name = _name; }  template <class type, int max> const std::string & group<type, max>::get_name() {   return this->name; } 

i implemented person class , employee class (which inherits person) , both work , have show() method.

my main looks this:

test.cc

#include <iostream> #include "group.h" // has declarations , implementation person/employee  int main (int argc, char const *argv[]) {     // person ctor takes name , age     person p1("john", 25);      person p2("jim", 29);      // group takes name init     group <person, 5> g("ozcorp");     g.add_member(p1);     g.add_member(p2);     g.list();     } 

i compiled simple makefile:

test: test.cc group.o     g++ -o test test.cc group.o  group.o: group.h group.cc     g++ -c group.cc 

and (whew), when ran ./test got following errors:

undefined symbols:   "group<person, 5>::list()", referenced from:       _main in ccaljrrc.o   "group<person, 5>::group(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:       groups()    in ccaljrrc.o       _main in ccaljrrc.o   "group<person, 5>::~group()", referenced from:       groups()    in ccaljrrc.o       _main in ccaljrrc.o       _main in ccaljrrc.o   "group<person, 5>::add_member(person&)", referenced from:       _main in ccaljrrc.o       _main in ccaljrrc.o ld: symbol(s) not found collect2: ld returned 1 exit status make: *** [test] error 1 

if made far - - , i'd appreciate insight on why happens. tried share as possible code (obviously), forgive me if it's way much. source compiled g++ 4.2.1 on mac osx 10.6.4. also, style / coding-habits tips appreciated. thanks!

templated class member definition not go .cc / .cpp files. go in .h, or in .hpp / .hxx file included .h rational .cc / .cpp files used build objects files. however, templated code, objects cannot created before templated arguments substituted. therefore, implementation must available pieces of code instantiating them: in .h file.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -