Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;链接函数的未定义引用 我有一个C++项目链接的问题,我不知道出了什么问题。 代码的笑话_C++_Templates_G++ - Fatal编程技术网

C++;链接函数的未定义引用 我有一个C++项目链接的问题,我不知道出了什么问题。 代码的笑话

C++;链接函数的未定义引用 我有一个C++项目链接的问题,我不知道出了什么问题。 代码的笑话,c++,templates,g++,C++,Templates,G++,clitest.cpp #include <iostream> #include "node.h" using namespace std; int main(int argc, char** argv) { node<int> *ndNew = new node<int>(7); return 0; } 错误日志如下所示 clitest.cpp: In function ‘int main(int, char**)’: clitest.c

clitest.cpp

#include <iostream>
#include "node.h"
using namespace std;

int main(int argc, char** argv)
{
    node<int> *ndNew = new node<int>(7);
    return 0;
}
错误日志如下所示

clitest.cpp: In function ‘int main(int, char**)’:
clitest.cpp:8:16: warning: unused variable ‘ndNew’ [-Wunused-variable]
     node<int> *ndNew = new node<int>(7);
                ^
/tmp/cc258ryG.o: In function `main':
clitest.cpp:8: undefined reference to `node<int>::node(int const&)'
collect2: error: ld returned 1 exit status
make: *** [blist] Error 1
clitest.cpp:在函数“int main(int,char**)”中:
clitest.cpp:8:16:警告:未使用的变量“ndNew”[-Wunused variable]
node*ndNew=新节点(7);
^
/tmp/cc258ryG.o:在函数“main”中:
clitest.cpp:8:对“node::node(int const&)”的未定义引用
collect2:错误:ld返回了1个退出状态
make:**[blist]错误1

我花了相当多的时间来转换代码,试图找出问题,我要么错过了一些基本的东西,要么就是我不知道C++链接的东西。p> 在.cpp文件之前使用
-I.
,以便编译器知道如何查找.h文件

g++ -Wall -I. clitest.cpp node.cpp -o clitest
或者只是
-I

g++ -Wall -I clitest.cpp node.cpp -o clitest

使用模板时,编译器需要知道如何在实例化类时为类生成代码。导致未定义引用错误的原因是编译器未生成
node::node(int const&)
构造函数。参见,例如

您有两个选择:

  • 将实现放在node.h中(node.cpp因不需要而被删除)
  • 将实现放在node.h底部包含的文件中(通常该文件称为node.tpp)
  • 我建议将实现放在node.h中并删除node.cpp。请注意,示例中的代码不是有效的c++:成员变量vecSons不是指针,因此行
    vecSons=new vector()
    将给出编译器错误。以下代码可以作为完整实现的起点:

    #ifndef NODE_H
    #define NODE_H
    #include <vector>
    
    template <typename T>
    class node
    {
        private:
            node<T>* ndFather;
            std::vector<node<T>* > vecSons;
        public:
            const T* Data;
            node(const T &d) : 
                ndFather(0),
                vecSons(),
                Data(&d)
            {
            }
    };
    #endif
    
    \ifndef节点
    #定义节点
    #包括
    模板
    类节点
    {
    私人:
    节点*ndFather;
    std::向量向量向量;
    公众:
    常数*数据;
    节点(常数T&d):
    ndFather(0),
    维克森(),
    数据(&d)
    {
    }
    };
    #恩迪夫
    
    链接器可能重复的答案是:clitest.cpp:(.text+0x31):未定义对'node::node(int const&')的引用。我真的不知道我从未听说过的额外标志是否有任何不同。通过使用仅标题的方法解决(我个人不喜欢)。
    g++ -Wall -I. clitest.cpp node.cpp -o clitest
    
    g++ -Wall -I clitest.cpp node.cpp -o clitest
    
    #ifndef NODE_H
    #define NODE_H
    #include <vector>
    
    template <typename T>
    class node
    {
        private:
            node<T>* ndFather;
            std::vector<node<T>* > vecSons;
        public:
            const T* Data;
            node(const T &d) : 
                ndFather(0),
                vecSons(),
                Data(&d)
            {
            }
    };
    #endif