使用模板时出现 g++ 重复符号错误(菜鸟问题)

2023-12-24

因此,我尝试选择 C++,为此我决定使用模板编写一个通用 Group 类,该类采用类型和大小作为模板参数:

在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

组抄送:

/**********************************************************
 * 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 类(继承自 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();    
}

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

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

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

最后(哇),当我运行它时./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

如果你做到了这一点 - 谢谢 - 我希望能了解一些关于为什么会发生这种情况的见解。我试图尽可能多地分享代码(显然),所以如果太多了,请原谅我。源代码是在 mac osx 10.6.4 上使用 g++ 4.2.1 编译的。此外,任何风格/良好的编码习惯技巧都将受到赞赏。谢谢!


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用模板时出现 g++ 重复符号错误(菜鸟问题) 的相关文章

随机推荐

  • 页面加载动画

    我想在页面加载时显示加载动画 并在加载完成后明显隐藏它 我正在使用 Masterpages 在 ASP NET 中工作 只是想知道有一种使用 JQuery 执行此操作的简单方法吗 任何指示将不胜感激 Thanks 只需将带有 gif 动画的
  • 如何针对 firebase 验证身份验证令牌?

    我的意思不是使用 firebase 进行自定义身份验证 我需要的与在应用程序服务器中生成令牌并允许在 firebase 中访问的自定义身份验证略有不同 实际上 例如 我正在尝试使用电子邮件和密码在 firebase 中进行身份验证 并且通过
  • Django 1.7 google oauth2 令牌验证失败

    我正在尝试完成验证 Google 令牌的过程 以便在 Django 应用程序中访问用户的日历 尽管我遵循了网上找到的一些指示 但我的回调函数仍然收到 400 错误代码响应 错误请求 views py coding utf 8 import
  • 通过嵌入插入的组件的 Angular 2+ 调用函数(ng-content)

    我有一个模态窗口组件 并且我正在通过嵌入插入模态内容组件
  • PrimeFaces 2,如何将ajax与h:selectOneBooleanCheckbox一起使用?

    我有一个 JSF PrimeFaces 2 x UI 带有一个复选框 h selectOneBooleanCheckbox 其值会影响其他小部件 就像是 X checkbox1 V combobox1 X checkbox2 当 check
  • AWS Glue DPU 配置

    我看到 DPU 由 4 个 vCPU 和 16 GB 内存组成 是否可以更改 vCPU 内存的设置 以便我不会用完 DPU 或超出 DPU 限制 我认为一个开发端点最多有 5 个 DPU 一个帐户最多有 2 个 DEV 端点 Regards
  • 在 HTTP/2 中使用图像精灵有意义吗?

    JS 和 CSS 文件的捆绑在 HTTP 2 中不需要 https stackoverflow com questions 30861591 why bundle optimizations are no longer a concern
  • 将时间字符串转换为日期格式 iOS

    我有一个字符串 21 57 我想在 NSDate 中以相同的 HH mm 格式保存它 我尝试使用以下方法转换它 NSDateFormatter dateFormatter NSDateFormatter alloc init dateFor
  • log4j2 双美元 $$ 符号在配置中的含义

    我正在阅读Log4j2的配置部分 http logging apache org log4j 2 x manual configuration html http logging apache org log4j 2 x manual co
  • 根据另一列过滤一列中的数据值,然后将值插入到同一个 SQL 表中的不同列中

    这是我试图使用 SSIS 和条件分割转换来解决的一个难题 我有一个 csv 文件 其中一行中包含每个唯一用户的属性数据 另一列中包含每个属性的值 IE Attribute Attribute Type ID 0000000001 Birth
  • 在不使用数组控制器的情况下,Ember 模型中如何实现排序?

    每个谷歌结果都是关于 ArrayController 排序的 需要一个排序机制而不使用ArrayController 有一个模型 其中有排序参数 就像说 sortOrder 作为模型中的属性之一 这将来自后端 将使用 each 渲染此模型
  • 如何删除www. ASP.NET MVC 中的前缀

    如何删除 www 来自传入的请求 我需要设置 301 重定向还是只需重写路径 不管怎样 最好的方法是什么 Thanks 我相信使用 IIS 的 URL 重写模块来做到这一点会更合适 如果您有权访问 IIS 的管理工具 则可以在站点设置的 I
  • 如何在MATLAB中为随机数生成器设置统一种子?

    我正在编写代码并使用 MATLAB 中的现有函数 如果这些函数使用随机数生成器怎么办 有没有办法可以修复这些函数的种子而无需更改它们的代码 MATLAB 中有执行此操作的命令吗 通常代码会使用 Matlab 的内置随机数生成器 您可以使用以
  • Sublime2 和 SublimeREPL

    使用 Windows 7 Python 3 2 和 Sublime Text 2 我完成了安装 SublimeREPL 的所有说明 当我转到Tools gt SublimeREPL gt Python gt Python我收到错误 Wind
  • 使用 jest.mock 传递超出范围的变量[重复]

    这个问题在这里已经有答案了 我有一个用于模拟的模拟对象react native const MyMock MockA methodA jest genMockFn MockB ObjectB methodA jest genMockFn m
  • 拍摄/选择图片并将其显示在 ImageView 中而不先保存(使用 MvvmCross)

    我想从库中选择一张图片或用相机拍照并将结果显示到视图 ImageView 但根据一些帖子 包括this one https stackoverflow com questions 13475896 need an example of ta
  • iReport - 组织列输出?

    I am working on a profit and loss report that should look like this And my data table looks like this 对于此损益表 我有查询 1 它填充当
  • webRTC 设置信令服务器

    当系统连接在局域网中时 如何为webRTC设置信令服务器 是否强制要求我们必须使用 STUN 和 TURN 服务器进行信令 要使 WebRTC 在 LAN 上运行 您需要在该 LAN 中拥有一个信令服务器 信令服务器是任何 Web 服务器
  • Z3中的parthood定义

    我试图在 Z3 中定义集合对 使用数组定义 之间的部分关系 在下面的代码中称为 C 我写了 3 个断言来定义自反性 传递性和反对称性 但 Z3 返回 未知 我不明白为什么 define sort Set Array Int Bool dec
  • 使用模板时出现 g++ 重复符号错误(菜鸟问题)

    因此 我尝试选择 C 为此我决定使用模板编写一个通用 Group 类 该类采用类型和大小作为模板参数 在group h中 ifndef GROUP H define GROUP H define MAX SIZE 10 Define a G