昨天写了个广义表,写完后用clang++编译,结果给我报了一个这样的错

tanglizi@archlinux ~/Code/cpp/DS/genlist $ clang++ main.cpp genlist.cpp -o main

/tmp/main-9e993f.o: In function `GenList<long>::GenList(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
main.cpp:(.text._ZN7GenListIlEC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7GenListIlEC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x19): undefined reference to `GenList<long>::update(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

可以看出是连接器找不到函数的实现

这就很难受了,明明是常见的类,咋就编译不过

这是我的文件(简略写了)

// main.cpp
#include "genlist.h"
#include <string>
using namespace std; int main(void){
GenList<long> list(string("(1, 2, (3, 4), ())")); //关键在这,连接器找不到构造函数的实现 return 0;
}
// genlist.h
#ifndef _GENLIST_H_
#define _GENLIST_H_ #include "genlistnode.h"
#include <iostream>
#include <string>
using namespace std; template <class T>
class GenList{
public:
GenList(const string &input){update(input)}; //看这里
...
private:
...
}; #endif // _GENLIST_H_
// genlist.cpp
#include "genlist.h" template <class T>
void GenList<T>::update(const string &input){ // 实现在这里
if (input[0]=='('){
int idx=1;
head=createGenList(input, idx);
}else
throw "Expresion Error!";
}

该实现的都实现了,就是编译不过

最后发现当我把模板去掉后,就可以编译通过了

这是为啥?

因为编译器在编译的时候,按需要把模板类换成一般类来编译

然而g++不允许把模板类定义和模板类方法实现分离编译,最后就是连接器报错undefined reference

解决方法就是将模板类和方法实现放在同一个头文件里编译

好气

[Bug]C++ XXX:undefined reference to "xxx"的更多相关文章

  1. C++ template error: undefined reference to XXX

    一般来说,写C++程序时推荐“类的声明和实现分离”,也就是说一个类的声明放在example.h文件中,而这个类的实现放在example.cpp文件中,这样方便管理,条理清晰. 但是如果类的声明用到了模 ...

  2. Linux下编译程序时,经常会遇到“undefined reference to XXX” 报错,

    Linux下编译程序时,经常会遇到“undefined reference to XXX” 报错, 这里总结一些可能的原因和解决方案,给需要的朋友: 说道undefined reference err ...

  3. undefined reference to XXX 问题原因

    原文地址:http://blog.csdn.net/cserchen/article/details/5503556 Linux下编译程序时,经常会遇到“undefined reference to ...

  4. cmake 出现undefined reference to xxx 解决办法

    cmake没怎么用,主要觉得Clion很好用,但是默认clion使用的是cmake.再说一句clion是linux平台上很好用,个人强推. 当你使用clion的时候,如果使用了thread cstl等 ...

  5. error: undefined reference to `XXX::XXX(type1, ypte2)

    moc_fortunethread.cpp:100: error: undefined reference to `FortuneThread::GetToParentThread(QString, ...

  6. Vivado SDK ,调用math.h函数的时候出现 undefined reference to `xxx' ,解决方案

    在Vivado SDK进行软件设计的时候,如调用math.h函数的时候出现 undefined reference to `sqrt' ,原因有以下情况: 1.没有添加需调用的头文件 解决方案:添加对 ...

  7. Fix: Compile project encounter undefined reference to“xxx”error

    Need to add all the new cpp files to jni/Andriod.mk folder:

  8. NDK 提示"undefined reference to xxx“的解决办法

    在Android.mk文件的 LOCAL_SRC_FILES后面加入包含该类或函数的文件,用\隔开,\后换行继续添加 例如 LOCAL_SRC_FILES := NDKTest.cpp\bncore. ...

  9. 编译中出现的undefined reference to XXX

    主要是在链接时发现找不到某个函数的实现文件.可以查找是否在makefile里没有增加待编译的.c文件,或者静态库没有引用

随机推荐

  1. hiho169周 - 表达式求值

    题目链接 计算表达式100*(2+12)-(20/3)*2 ---------------------------------------------------------------------- ...

  2. echars自定义提示框位置

    tooltip: { trigger: 'item', formatter: "{a} <br/>{b}: {c} ({d}%)", position:function ...

  3. WebRTC | Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=msid: Missing track ID in msid attribute.

    1.问题回放 使用如下代码获取局域网IP报错 (代码来源:https://github.com/diafygi/webrtc-ips 日期:2019-02-16) Uncaught (in promi ...

  4. 一:1.2【print&input与变量和运算符】

    [路径] 绝对路径:从根目录开始链接的路径  --->cd C:\Windows\Boot\DVD\EFI\en-US 相对路径:不从根目录开始链接的路径 ----> cd Boot\DV ...

  5. WPF 封装 dotnet remoting 调用其他进程

    原文:WPF 封装 dotnet remoting 调用其他进程 本文告诉大家一个封装好的库,使用这个库可以快速搭建多进程相互使用. 目录 创建端口 调用软件 运行的类 运行C++程序 通道 使用 在 ...

  6. 虚拟机VM安装Linux系统CentOS7

    第一步:安装一个VM虚拟机: 百度VM,使用普通下载,一路Next即可 如果需要输入序列号,可以网上随意找一个,目前是个人可以随意激活,但如果做商业用途的话,还是最好买一个序列号,我在网上搜到的:5A ...

  7. FS,FT,DFT,DFS和DTFT的关系

    对于初学数字信号(Digital Signal Processing,DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. FS:时域上任意连续的周期信号可以分 ...

  8. [MST] Attach Behavior to mobx-state-tree Models Using Actions

    Models are not just a nifty feature for type checking. They enable you to attach behavior to your ac ...

  9. Django Admin site 显示问题

    Django Admin site 显示问题 今天配置了一下Django admin site,可是admin site的显示有一些问题,当我打开源码.訪问里面的admin 的css 文件时候,htt ...

  10. poj_2828线段树,逆序插入

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...