首页 > 解决方案 > C++ 未解析的外部符号

问题描述

我尝试使用构造函数将类对象创建到另一个类中,但出现“无法解析的外部符号”错误。我研究示例代码,但在我的代码中找不到任何错误。

链表.h

#pragma once
#include <cstdint>
#include <list>
#include <iterator>
using namespace std;


template  <class T> class LinkedList
{
public:
    LinkedList();
    LinkedList(int Size);   
    list <T> data;
    int size;
    LinkedList* next;

};

链表.cpp

#include "LinkedList.h"

template <class T> 
LinkedList <T> ::LinkedList() {

    size = 0;
    next = NULL;
}

template <class T>
LinkedList <T> ::LinkedList(int Size) {

    size = Size;
    next = NULL;
}

Cotp_connection.h

#pragma once
#include "connect_tcp.h"
#include "LinkedList.h"

class Cotp_connection {
public:
    Cotp_connection();
    Cotp_connection(int size);  
};

Cotp_connection.cpp

#include "Cotp_connection.h"

Cotp_connection::Cotp_connection() {
    LinkedList <uint8_t> list;
}

Cotp_connection::Cotp_connection(int size) {

    LinkedList <uint8_t> list(size);
}

LNK2019 未解析的外部符号“public: __thiscall LinkedList::LinkedList(void)” (??0?$LinkedList@E@@QAE@XZ) 在函数“public: __thiscall Cotp_connection::Cotp_connection(void)” (??0Cotp_connection @@QAE@XZ) kkkkk_v2

编辑:如果我以这种方式编辑我的代码,它可以工作。Okey,但为什么它现在可以工作,但以前不行?

Cotp_connection.cpp

Cotp_connection::Cotp_connection() {
    LinkedList <uint8_t> list();
}

Cotp_connection::Cotp_connection(int size) {

    LinkedList <uint8_t> list((size));
}   

链表.h

#pragma once
#include <cstdint>
#include <list>
#include <iterator>
using namespace std;


template  <class T> class LinkedList
{
public:
    LinkedList();
    LinkedList(int Size) :size(Size),next(NULL) {}; 
    list <T> data;
    int size;
    LinkedList* next;

};

标签: c++constructor

解决方案


推荐阅读