【问题标题】:ifstream or ofstream types called by template function模板函数调用的 ifstream 或 ofstream 类型
【发布时间】:2014-12-30 11:38:37
【问题描述】:

我发布了完整的源代码,很抱歉之前没有这样做。

myclass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

#include <cstring>
#include <iostream>
#include <fstream>

class MyClass
{
    public:

        /*
         * @Brief Constructor
         */
        MyClass();

        /*
         * @Brief Constructor
         * @param orig
         */
        MyClass(const MyClass & orig);

        /*
         * @Brief Destructor
         */
        virtual ~MyClass();

        template<typename T> bool openFile(T &file);

    private:

};

template <typename T>
class streamType
{
public:
        static bool isInStream() { return false; }
};

//
// Specialized for the ifstream
//
template<>
class streamType<std::ifstream>
{
public:
        static bool isInStream() { return true; }
};


#endif

myclass.cpp:

#include "myclass.h"

MyClass::MyClass()
{
}

MyClass::MyClass(const MyClass & orig)
{
}

MyClass::~MyClass()
{
}

template<typename T> bool MyClass::openFile(T &file)  
{
    std::string path, filename;

    if(streamType<T>::isInStream())
    {
        file.open(path + filename, std::ios::in | std::ios::binary | std::ios::ate);
    }
    else
    {
        file.open(path + filename, std::ios::out | std::ios::binary | std::ios::ate);
    }
}

主要:

#include <cstdlib>
#include "myclass.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    std::ifstream in;
    std::ofstream out;
    MyClass foo;

    if(foo.openFile(in))  //reading
    {
        // do something
        in.close();
    }
    if(foo.openFile(out))  // writing
    {   
        // do something
        out.close();  
    }

    return 0;
}

目前我有两个错误,但之前只有一个 ....???迷茫

编译器错误: 未定义对“bool MyClass::openFile > >(std::basic_ifstream >&)”的引用 未定义对“bool myfile::openFile > >(std::basic_ofstream >&)”的引用 make[2]: *** [dist/Debug/Intel-Linux-x86/test] 错误 1

Gg

【问题讨论】:

  • 错误应该被阅读
  • 您是否记得只将模板放在头文件中?
  • 所以这是一个链接器错误。我怀疑你在 cpp 中定义了模板函数。
  • 是的,在同一个.h头文件中定义:“openFile(T &file)”,模板 class streamType”和“template class streamType<:ifstream>”
  • 声明定义需要在header文件中。您不应该为 MyClass 使用 .cpp 文件。

标签: c++ templates compiler-errors fstream


【解决方案1】:

myclass.h 中删除openFile() 的定义。然后在myclass.cpp 中,将函数的声明替换为其定义,如下所示:

    template<typename T>
    bool openFile(T &file)
    {
        std::string path, filename;

        if(streamType<T>::isInStream())
        {
            file.open(path + filename, std::ios::in | std::ios::binary | std::ios::ate);
        }
        else
        {
            file.open(path + filename, std::ios::out | std::ios::binary | std::ios::ate);
        }
        // you should return something in a fuunction returning a bool
    }

正如0x499602D2所说,声明和定义需要在头文件中。

但是,现在这一行

if(streamType&lt;T&gt;::isInStream())

会导致这样的错误:

error: ‘streamType’ was not declared in this scope
error: expected primary-expression before ‘>’ token
error: ‘::isInStream’ has not been declared

我不确定streamType 是否是 C++,它看起来像 C# 功能,但这是一个不同的问题。

【讨论】:

    【解决方案2】:

    声明和定义需要在头文件中。你 不应为 MyClass 使用 .cpp 文件。 – 0x499602D2 2 小时 以前

    是正确答案!!

                                        or
    

    将模板函数的声明和定义放在头文件中,将其他类函数留在.cpp中,因为如上所述

    Piotr S.“模板函数只在头文件中”。

    这也是正确的! 谢谢大家

    我知道将模板放入 .cpp 文件中是有技巧的,但在我看来,它不仅仅是模板。

    技巧:在 .cpp 文件中使用 #define currentType T。

    【讨论】:

      猜你喜欢
      • 2012-09-19
      • 1970-01-01
      • 2015-08-03
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2012-03-06
      相关资源
      最近更新 更多