0

Source.cpp

#include <iostream>
#include <string>
#include <vector>

#include "Header.h"

using std::endl;
using std::cout;
using std::cin;
using std::string;
using std::vector;

void main() {

    Test<float> testy(10, 100);
    cout << testy.getTest1() << endl;

    system("PAUSE");

}

Header.h

#pragma once

template<typename T>
class Test
{

private:
    T test1, test2;

public:
    Test(T a, T b);
    ~Test();

    T getTest1();
    T getTest2();

};

Header.cpp

#include "Header.h"

template<typename T>
Test<T>::Test(T a, T b)
{
    test1 = a, test2 = b;
}

template<typename T>
Test<T>::~Test()
{

}

template<typename T>
T Test<T>::getTest1()
{
    return test1;
}

template<typename T>
T Test<T>::getTest2()
{
    return test2;
}

So I am lost, whenever I F5, I get error of this...

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall Test::Test(float,float)" (??0?$Test@M@@QAE@MM@Z) referenced in function _main Tutorial Stuff C:\Users\a??\documents\visual studio 2015\Projects\Tutorial Stuff\Tutorial Stuff\Source.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall Test::~Test(void)" (??1?$Test@M@@QAE@XZ) referenced in function _main Tutorial Stuff C:\Users\a??\documents\visual studio 2015\Projects\Tutorial Stuff\Tutorial Stuff\Source.obj 1

Error LNK2019 unresolved external symbol "public: float __thiscall Test::getTest1(void)" (?getTest1@?$Test@M@@QAEMXZ) referenced in function _main Tutorial Stuff C:\Users\a??\documents\visual studio 2015\Projects\Tutorial Stuff\Tutorial Stuff\Source.obj 1

Error LNK1120 3 unresolved externals Tutorial Stuff C:\Users\a??\documents\visual studio 2015\Projects\Tutorial Stuff\Debug\Tutorial Stuff.exe 1

I'm assuming I'm not linking it right, from what the error is telling me, but I double double check how its done, don't know now what the problem is.

Moorease
  • 13
  • 4
  • `#include "Header.cpp"` in Source.cpp. – macroland Jun 10 '21 at 10:51
  • Hey THANKS! that actually solved it! haha. I just finished a mini project, was so used to thinking that just adding headers from another file is fine. I was so used to that, that in this case of templates I have to use the .h AND the .cpp file. I don't know why that is but now I know the solution thanks! – Moorease Jun 10 '21 at 11:03
  • It is best not to separate templates into .h and .cpp files and rather implement it in a related single .hpp file. – macroland Jun 10 '21 at 13:52

0 Answers0