gpt4 book ai didi

C++ 链接失败, undefined reference

转载 作者:太空狗 更新时间:2023-10-29 23:19:04 29 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why can templates only be implemented in the header file?
Undefined reference to function template when used with string (GCC)
C++ templates, undefined reference

我觉得我缺少链接 C++ 项目的内容。我不确定问题是出在 header 源中还是包含在其中,所以我制作了一个最小的代码示例来演示它。

主模块最小维护.cpp:

 #include <stdio.h>
#include <vector>
#include <string>
#include "nodemin.h"

using namespace std;

int main()
{
// Blist libs are included
Node<int>* ndNew = ndNew->Root(2);

return 0;
}

头文件nodemin.h:

 #ifndef NODETEMP_H_
#define NODETEMP_H_

using namespace std;

template<class T>
class Node
{
protected:

Node* m_ndFather;
vector<Node*> m_vecSons;
T m_Content;

Node(Node* ndFather, T Content);

public:

// Creates a node to serve as a root
static Node<T>* Root(T RootTitle);
};

#endif

节点模块nodemin.cpp:

 #include <iostream>
#include <string.h>
#include <vector>
#include "nodemin.h"

using namespace std;

template <class T>
Node<T>::Node(Node* ndFather, T Content)
{
this->m_ndFather = ndFather;
this->m_Content = Content;
}

template <class T>
Node<T>* Node<T>::Root(T RootTitle) { return(new Node(0, RootTitle)); }

编译行:

 #g++ -Wall -g mainmin.cpp nodemin.cpp

输出:

 /tmp/ccMI0eNd.o: In function `main':
/home/******/projects/.../src/Node/mainmin.cpp:11: undefined reference to`Node<int>::Root(int)'
collect2: error: ld returned 1 exit status

我尝试编译成对象,但链接仍然失败。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com