【问题标题】:undefined reference to 'std::string Helper::ToString<int>(int const&)'未定义对 'std::string Helper::ToString<int>(int const&)' 的引用
【发布时间】:2016-06-22 20:31:06
【问题描述】:

我得到错误:

对 'std::string Helper::ToString 的未定义引用 (int const&)' 在 Helper.h 中的 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;
}

【问题讨论】:

  • 缩进不是很清楚,但看起来您正试图在命名空间Helper 范围之外定义Helper::ToString。或者,换句话说,Helper::ToString 已声明但未定义。

标签: c++


【解决方案1】:

将实现绑定到您的命名空间:

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

【讨论】:

    【解决方案2】:

    您已经在命名空间Helper 中声明了方法Helper::ToString,但没有在任何地方定义它。命名空间外的Helper.h 中的ToString 方法的定义对命名空间内的声明一无所知。

    要么按照其他答案的建议绑定实现,要么在您声明它的 Helper 命名空间本身内定义此方法。

    【讨论】:

      【解决方案3】:

      头文件中需要放这部分

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

      进入“命名空间助手”的边界。目前,它在外面。 同样,您需要在“namespace Helper”的结尾大括号之后删除一个半列。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-18
        • 2015-10-23
        • 2020-09-17
        • 1970-01-01
        • 2011-07-20
        • 1970-01-01
        相关资源
        最近更新 更多