gpt4 book ai didi

c++ - 使用模板时 undefined reference

转载 作者:行者123 更新时间:2023-11-28 05:52:12 26 4
gpt4 key购买 nike

<分区>

我有一个名为 DataHandler模板化类

#ifndef DATAHANDLER_H
#define DATAHANDLER_H
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <set>
#include "constants.h"

template <typename T>
using Car = std::pair< T, T>;

template <typename T>
using SparseMatrix = std::vector< Car<T> >;

template <class T>
class DataHandler
{

public:
// initializes a new DataHandler only if none has been created,
// otherwise return the living instance
static DataHandler<T>* getInstance()
{
if(!dataHandler)
dataHandler = new DataHandler();
return dataHandler;
}

void readFile();

SparseMatrix<T>* getSparseBlue(){ return &sparseBlue; }
SparseMatrix<T>* getSparseRed(){ return &sparseRed; }
virtual ~DataHandler();

private:
// static DataHandler to ensure only one instance can be created
static DataHandler<T> *dataHandler;

// private constructor to use DataHandler as a Singleton
DataHandler();

int numElem = 0;
int m_rows, m_cols = -1;


#endif // DATAHANDLER_H

文件是:

#include "data_handler.h"
#include <fstream>
#include <algorithm>
#include <omp.h>
#include <chrono>

using namespace std;
using namespace constants;

// Global static pointer used to ensure a single instance of the class.
template<typename T>
DataHandler<T>* DataHandler<T>::dataHandler = NULL;

template<typename T>
DataHandler<T>::DataHandler()
{
//ctor
}

template<typename T>
DataHandler<T>::~DataHandler()
{
//dtor
}

template<typename T>
void DataHandler<T>::readFile()
{
// do some stuff
}

// Instantiation of relevant templates
template class DataHandler<unsigned char>;
template class DataHandler<unsigned short int>;

在最后两行中,我实例化了我在 main.cpp 中定义的模板。 :

#include <iostream>
#include <chrono>
#include <fstream>
#include <algorithm>

#include "data_handler.h"
#include "dense_traffic_handler.h"
#include "sparse_traffic_handler.h"
#include "constants.h"

using namespace std;

// Check the number of rows/cols to choose between char or short int for the sparse case
bool matrixIsSmall()
{
return true;
}
void integerCase()
{
typedef unsigned char T;
DataHandler<T> *dh = DataHandler<T>::getInstance();
dh->readFile();

DenseTrafficHandler dth(dh); // ****** ERROR HERE *****
}

void charCase()
{
typedef unsigned char T;
DataHandler<T> *dh = DataHandler<T>::getInstance();
dh->readFile();

DenseTrafficHandler dth(dh); // ****** ERROR HERE *****
SparseTrafficHandler<T> sth;

set<unsigned short int> step = dh->getstep();

int currentStep = 0;
set<unsigned short int>::const_iterator stepToSave = step.begin();
}

int main(int argc, char *argv[])
{
if(matrixIsSmall())
charCase();
else
integerCase();

return 0;
}

编译器给我一个错误: undefined reference to DenseTrafficHandler::DenseTrafficHandler<unsigned short>(DataHandler<unsigned short>*)

DenseTrafficHandler header 是这样的:

#ifndef TRAFFICHANDLER_H
#define TRAFFICHANDLER_H

#include "constants.h"
#include "data_handler.h"

class DenseTrafficHandler
{
public:
template<typename T>
DenseTrafficHandler(DataHandler<T> *dh);
virtual ~DenseTrafficHandler();
private:
int m_cols, m_rows;
char* data;
char ** dense = NULL;
};

#endif // TRAFFICHANDLER_H

DenseTrafficHandler 是:

#include "dense_traffic_handler.h"

using namespace std;
using namespace constants;

template <typename T>
DenseTrafficHandler::DenseTrafficHandler(DataHandler<T> *datah)
{
DataHandler<T> *dh = datah;
dense = dh->getDense();
m_rows = dh->getm_rows();
m_cols = dh->getm_cols();
}
DenseTrafficHandler::~DenseTrafficHandler()
{
//dtor
}

所以我有两个问题:

  1. 我为什么会收到此错误,我该如何处理?
  2. DataHandler 源代码中有没有办法不指定 template <typename T>
    DataHandler<T>::functionName()
    每个功能? (我的意思是像 using namespace Datahandler<T> )

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