Understanding template classes in C ++ - problem with new operator

Dear, I have already found this problem for several days, and my searches were unsuccessful.

What I'm trying to do: I want the template reader class (VariableReader) to handle different types of variables (usually unsigned int and vector pointers).

I started with

#ifndef READER_H_
#define READER_H_
#include <string>

namespace BAT {
template <typename variableType = unsigned int>
class VariableReader {
public:
 VariableReader<variableType>();
 VariableReader<variableType>(std::string varName);
 virtual ~VariableReader<variableType>();
 std::string getVariableName();
 void setVariableName(std::string varName);
 bool isValidVariableName(std::string varName);
 variableType getVariable();
private:
 std::string variableName;
 variableType variable;

};

}

#endif

and

#include "../../interface/Readers/VariableReader.h"

namespace BAT {

template<typename variableType>
VariableReader<variableType>::VariableReader() :
 variableName("") {
 // TODO Auto-generated constructor stub
}

template <typename variableType>
VariableReader<variableType>::VariableReader(std::string varName) :
 variableName(varName) {

}

template <typename variableType>
std::string VariableReader<variableType>::getVariableName() {
 return variableName;
}

template <typename variableType>
void VariableReader<variableType>::setVariableName(std::string varName) {
 if (VariableReader::isValidVariableName(varName)) {
  variableName = varName;
 }
}

template <typename variableType>
bool VariableReader<variableType>::isValidVariableName(std::string varName) {
 return varName != "";
}

template <typename variableType>
VariableReader<variableType>::~VariableReader() {
 // TODO Auto-generated destructor stub
}

}

However, although it seems to be compiling, I cannot use it in other projects. EDIT: forgot to post test code:

#include "cute.h"
#include "ide_listener.h"
#include "cute_runner.h"

#include "Readers/VariableReader.h"
using namespace BAT;

static VariableReader<int> *reader;

void setUp(){
reader = new VariableReader<int>::VariableReader();//this is problem-line
}

void thisIsATest() {
    ASSERTM("start writing tests", false);  
}

void runSuite(){
    cute::suite s;
    //TODO add your test here
    s.push_back(CUTE(thisIsATest));
    cute::ide_listener lis;
    cute::makeRunner(lis)(s, "The Suite");
}

int main(){
    runSuite();
}

The following error message appears:

Building target: BAT_Tests
Invoking: GCC C++ Linker
g++ -L"/workspace/BAT/Debug Gcov" -fprofile-arcs -ftest-coverage -std=c99 -o"BAT_Tests"  ./src/Test.o   -lBAT
./src/Test.o: In function `setUp()':
/workspace/BAT_Tests/Debug Gcov/../src/Test.cpp:13: undefined reference to `BAT::VariableReader<int>::VariableReader()'
collect2: ld returned 1 exit status
make: *** [BAT_Tests] Error 1

As I understand it, the linker is trying to find a constructor for VariableReader, which is not explicitly defined, since I want to have only a common constructor.

Please help me understand what I am missing.

+3
2

++ FAQ Lite ? :

  • .h ( , .h).
  • .cpp template VariableReader<unsigned int>;.
+6

() . , , , - , , .

+3

Source: https://habr.com/ru/post/1752061/


All Articles