【问题标题】:C++ Class Template Issue [duplicate]C ++类模板问题[重复]
【发布时间】:2014-05-06 09:33:59
【问题描述】:

我的程序出现“未解析的外部符号”错误,我尝试了所有我知道的方法,但无法解决。我使用类模板后开始出现此错误。

这是 FileHandler 类头文件:-

#include <string>
#include <iostream>
#include "test.h"
#include <iomanip>
#include "Administrator.h"
#ifndef  FileHandler_h
#define FileHandler_h

using namespace std;
template<class T>
class FileHandler
   {
     public:string writeToFile(T writeObject);

public: FileHandler();

   };
  #endif

文件处理类实现

template<class T>
FileHandler<T>::FileHandler(){}

template<class T>
string FileHandler<T>:: writeToFile(T writeObject)
{

   ofstream outputFile;
   outputFile.open("file.txt",ios::out|ios::app);


more code..........

     return "done";
 }

这是我在文件处理程序类中调用方法的代码

string Administrator::registerNewMember(Administrator newAdmin)
{

  FileHandler<Administrator> f1;

    return f1.writeToFile(newAdmin);

}

下面是我得到的错误的屏幕截图。

我该如何解决这个错误?

【问题讨论】:

  • 请复制错误文本。不要截屏。

标签: c++ templates file-io compiler-errors class-template


【解决方案1】:

将“文件处理程序类头”和“文件处理程序类实现”组合在一个文件中。

【讨论】:

    【解决方案2】:

    将来自.cpp 的定义放在头文件.h 中的声明旁边。或者您需要显式实例化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-05
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多