簡體   English   中英

未定義對&#39;std :: string Helper :: ToString的引用 <int> (int const&)&#39;

[英]undefined reference to 'std::string Helper::ToString<int>(int const&)'

我得到錯誤:

在ToString(y)行上對Helper.h中的'std :: string Helper :: ToString <int>(int const&)'的未定義引用:

助手

#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;
}

將實現綁定到您的名稱空間:

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

或將其內聯:

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

您已經在名稱空間Helper聲明了方法Helper::ToString ,但未在任何地方定義它。 命名空間之外的Helper.h ToString方法的定義對命名空間內的聲明一無所知。

如另一個答案所建議的那樣綁定實現,或者在您聲明它的Helper名稱空間本身內定義此方法。

在頭文件中,您需要將這一部分

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();
}

進入“命名空間助手”的邊界。 目前在外面。 同樣,您需要刪除“命名空間助手”結尾大括號后的半列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM