使用模板时g ++重复符号错误(noob问题)

sa1*_*125 3 c++ templates compiler-errors g++

所以我试图选择C++,为此我决定使用模板编写一个通用的Group类,它将Type和size作为模板参数:

在group.h中:

#ifndef __GROUP_H
#define __GROUP_H
#define MAX_SIZE 10

/**********************************************************
 * Define a Group class that handles a collection of members
 * of some 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
Run Code Online (Sandbox Code Playgroud)

group.cc:

/**********************************************************
 * class methods for 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 i = 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 - this Group is full!\n";
    }
}

template <class Type, int max>
void Group<Type, max>::list() {
    int i = 0;
    std::cout << "The following are members of the Group " << this->name <<":\n";
    // assumes the member has a show() method implemented
    while (i < this->count) {
        std::cout << i << ". ";
        (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;
}
Run Code Online (Sandbox Code Playgroud)

我还实现了一个Person类和一个Employee类(继承自Person),并且都工作并具有show()方法.

我的主要看起来像这样:

test.cc

#include <iostream>
#include "group.h" // this also has the declarations and implementation for Person/Employee

int main (int argc, char const *argv[])
{
    // Person ctor takes name and age
    Person p1("John", 25); 
    Person p2("Jim", 29);

    // Group takes name to init
    Group <Person, 5> g("Ozcorp");
    g.add_member(p1);
    g.add_member(p2);
    g.list();    
}
Run Code Online (Sandbox Code Playgroud)

我用一个简单的Makefile编译它:

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

group.o: group.h group.cc
    g++ -c group.cc
Run Code Online (Sandbox Code Playgroud)

最后(哇),当我运行它时遇到./test以下错误:

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
Run Code Online (Sandbox Code Playgroud)

如果你做到这一点 - 谢谢 - 我会很感激为什么会发生这种情况.我试图尽可能多地从代码中分享(显然),所以请原谅我,如果它的方式很多.Source是在mac osx 10.6.4上使用g ++ 4.2.1编译的.此外,任何风格/良好的编码习惯提示将不胜感激.谢谢!

Sch*_*ron 6

模板化类成员定义不会转到.cc/.cpp文件.它们进入.h或.h所包含的.hpp/.hxx文件.理性的是.cc/.cpp文件用于构建对象文件.但是,对于模板化代码,在替换模板化参数之前无法创建对象.因此,它们的实现必须可用于实例化它们的所有代码:在类似.h的文件中.