0

I'm new to C++ and I couldn't figure out how to fix this linker error...

My TagBase.h file:

#pragma once

#ifndef RRS_System
    #include <string>
#endif // RRS_System

namespace Ladder{



template<class T>
class TagBase
{
    protected:
        std::string name;
        T value;

    public:
        TagBase<T>(std::string tagName);
        TagBase<T>(std::string tagName, T startValue);

        virtual std::string ToString();
        std::string GetName();
        T GetValue();
        T* GetValuePtr();
};

}

My TagBase.cpp file:

#include "TagBase.h"

template<typename T>
Ladder::TagBase<T>::TagBase(std::string tagName) : name{tagName}
{
}

template<typename T>
Ladder::TagBase<T>::TagBase(std::string tagName, T startValue) : name{ tagName }, value{ startValue }
{
}

template<typename T>
std::string Ladder::TagBase<T>::ToString()
{
    return "Nome: " + TagBase<T>::name;
}

template<typename T>
std::string Ladder::TagBase<T>::GetName()
{
    return TagBase<T>::name;
}

template<typename T>
T Ladder::TagBase<T>::GetValue()
{
    return TagBase<T>::value;
}

template<typename T>
T* Ladder::TagBase<T>::GetValuePtr()
{
    return &value;
}

My main.cpp file:

#define RRS_System
#ifdef RRS_System
    #include <iostream>
    #include <string>
    #include "TagBase.h"    
#endif // RRS_System


#ifdef RRS_System
    int main() {
        Ladder::TagBase<int> t1 = Ladder::TagBase<int>::TagBase("ols");
        std::cout << t1.ToString();
        return 0;
    }
#endif // RRS_System

I'm getting this error:

1> MainTeste.obj : error LNK2019: unresolved external symbol "public: __thiscall Ladder::TagBase::TagBase(class std::basic_string<char,struct std::char_traits,class std::allocator >)" (??0?$TagBase@H@Ladder@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

1> MainTeste.obj : error LNK2019: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits,class std::allocator > __thiscall Ladder::TagBase::ToString(void)" (?ToString@?$TagBase@H@Ladder@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main

I know there are some similar questions, but non of them solve my problem...

Can anyone help pls?

  • If you're not sure what the dupe is about: Template must either go in the header or must be explicitly instantiated in the cpp for all types you will use it with. Template are what they are called: template of code. The compiler must will the holes (for example `T`) with a real type. If it cannot see the implementation, it can't instantiate the function definition, hence your linker error. – Guillaume Racicot Sep 30 '21 at 01:46

0 Answers0