0

I get the error:

undefined reference to 'std::string Helper::ToString<int> (int const&)' in Helper.h on line ToString(y):

Helper.h

#ifndef HELPER_H
#define HELPER_H

#include <ctime>
#include <string>
#include <sstream>
#include <fstream>

namespace Helper
{
    template <class T>

    std::string ToString(const T &);

    struct DateTime
    {
        DateTime()
        {
            time_t ms;
            time (&ms);

            struct tm *info = localtime(&ms);

            D = info->tm_mday;
            m = info->tm_mon + 1;
            y = 1900 + info->tm_year;
            M = info->tm_min;
            H = info->tm_hour;
            S = info->tm_sec;
        }

        DateTime(int D, int m, int y, int H, int M, int S) : D(D), m(m), y(y), H(H), M(M), S(S) {}
        DateTime(int D, int m, int y) : D(D), m(m), y(y), H(0), M(0), S(0) {}


    DateTime Now() const
    {
        return DateTime();
    }

    int D, m, y, H, M, S;


    std::string GetDateString() const
    {
        return std::string( D < 10 ? "0" : "") + ToString(D) +
               std::string( m < 10 ? ".0" : ".") + ToString(m) + "." +
               ToString(y);

    }

    std::string GetTimeString(const std::string &sep = ":") const
    {
        return std::string( H < 10 ? "0" : "") + ToString(H) + sep +
               std::string( M < 10 ? "0" : "") + ToString(M) + sep +
               std::string( S < 10 ? sep : "") + ToString(S);
    }

    std::string GetDateTimeString( const std::string &sep = ":") const
    {
        return GetDateString() + " " + GetTimeString(sep);
    }
    };
};
template <class T>

std::string ToString(const T &e)
{
    std::ostringstream s;
s << e;
return s.str();
}

void WriteAppLog( const std::string &s)
{
std::ofstream file ("AppLog.txt", std::ios::app);
file << "[" << Helper::DateTime().GetDateTimeString() << "]" <<
"\n" << s << std::endl << "\n";
file.close();
}


#endif // HELPER_H

KeyConstant.h

#ifndef KEYCONSTANT_H
#define KEYCONSTANT_H

#include <map>
#include <string>

class KeyPair
{
public:
    KeyPair (const std::string &vk = "", const std::string &name = "") : VKName (vk), Name (name) {}

std::string VKName;
std::string Name;
};

class Keys
{
public:
static std::map<int, KeyPair> KEYS;

};
#endif // KEYCONSTANTS_H

main.cpp

#include <iostream>
#include <windows.h>
#include "Helper.h"
#include "KeyConstant.h"
#include "Base64.h"

using namespace std;

int main ()
{
MSG Msg;

while (GetMessage (&Msg, NULL, 0, 0))
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
return 0;
}
1
  • 1
    The indentation isn't too clear but it looks like you are trying to define Helper::ToString outside the scope of the namespace Helper. Or, in other words, Helper::ToString is declared but not defined. Commented Jun 22, 2016 at 20:33

3 Answers 3

1

Bind the implementation to your namespace:

template <class T>
std::string Helper::ToString(const T &e)
         // ^^^^^^^^
{
    std::ostringstream s;
    s << e;
    return s.str();
}

or place it inline:

namespace Helper
{
    template <class T>
    std::string ToString(const T &e) {
         std::ostringstream s;
         s << e;
         return s.str();
    }
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have declared the method Helper::ToString in the namespace Helper but not defined it anywhere. The definition of the ToString method in Helper.h outside of the namespace knows nothing about the declaration inside the namespace.

Either bind the implementation as the other answer suggests, or define this method inside the Helper namespace itself where you have declared it.

Comments

0

In header file, you need to put this part

template <class T>

std::string ToString(const T &e)
{
    std::ostringstream s;
s << e;
return s.str();
}

void WriteAppLog( const std::string &s)
{
std::ofstream file ("AppLog.txt", std::ios::app);
file << "[" << Helper::DateTime().GetDateTimeString() << "]" <<
"\n" << s << std::endl << "\n";
file.close();
}

into the borders of "namespace Helper". Currently, it is outside. As well, you need to remove a semi column after "namespace Helper"'s ending curly bracket.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.