g ++重复符号错误时使用模板(noob问题) [英] g++ duplicate symbol error when working with templates (noob question)

查看:240
本文介绍了g ++重复符号错误时使用模板(noob问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to pick C++, and to do so I decided to write a generic Group class using templates, that takes a Type and size as template parameters:

in 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

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;
}



我还实现了一个Person类和一个Employee类并且都有工作和有show()方法。

I also implemented a Person class and an Employee class (which inherits from Person) and both work and have the show() method.

我的主要看起来像这样:

My main looks like this:

test.cc

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();    
}



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

I compiled it with a simple Makefile:

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

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

,当我用 ./ test 运行时出现以下错误:

And finally (whew), when I ran it with ./test got the 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

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

If you made it this far - thanks - and I'd appreciate some insight on why this happens. I tried to share as much as possible from the code (obviously), so forgive me if it's way to much. Source was compiled with g++ 4.2.1 on mac osx 10.6.4. Also, any style / good coding-habits tips will be appreciated. Thanks!

推荐答案

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

Templated class member definition does not go to .cc / .cpp files. They go in the .h, or in a .hpp / .hxx file included by the .h The rational is that .cc / .cpp files are used to build objects files. However, with templated code, objects cannot be created before templated arguments are substituted. Therefore, their implementation must be available to all pieces of code instantiating them: in a .h like file.

这篇关于g ++重复符号错误时使用模板(noob问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆