无法在头文件中声明ifstream类成员

无法在头文件中声明ifstream类成员

本文介绍了无法在头文件中声明ifstream类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图声明一个ifstream对象在头文件,如显示,但我得到一个错误,说它不能访问。我已经尝试过各种各样的事情,如使它成一个指针,初始化在.c文件等,但我的代码似乎不能得到它的声明。

I am trying to declare an ifstream object in a header file as is shown but I get an error saying that it cannot be accessed. I have tried various things such as making it into a pointer instead, initialising in the .c file etc. but my code can't seem to get part the declaration of it.

ReadFile.h:

ReadFile.h:

#ifndef READFILE_H
#define READFILE_H

#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <fstream>

class ReadFile{

private:
    std::ifstream stream;

public:
    std::string read();

    ReadFile();                                 // Default constructor
    ~ReadFile();                                    // Destructor
};

#endif

ReadFile.c:
#include ReadFile.h

ReadFile.c: #include "ReadFile.h"

ReadFile::ReadFile(){
stream.open("./data.txt");
}

ReadFile::~ReadFile(){
stream.close();
}

我得到的错误是:

Error   9   error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>' c:\users\Bob\documents\project\models\readfile.h    23  1   Project

输出为:

1>c:\users\Bob\documents\project\models\readfile.h(23): error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\fstream(827) : see declaration of 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'ReadFile::ReadFile(const ReadFile &)'

当包含 std :: ifstream stream; 时会发生错误,并且在删除此行时将会消失。可能是什么原因导致此错误?

The error occurs when std::ifstream stream; is included and will disappear when this line is removed. What could be causing this error? Have I missed something really obvious or is there more to it?

推荐答案

问题是 std: :ifstream 没有公共副本构造函数(因为复制一个构造函数没有意义),但是类的编译器生成的复制构造函数想要使用它。

The problem is that std::ifstream doesn't have a public copy constructor (because copying one wouldn't make sense) but the compiler-generated copy constructor for your class wants to use it.

由于同样的原因,它没有任何可用的赋值运算符(即复制 std :: ifstream 是无意义的)。

It doesn't have any available assignment operator for the same reason (i.e. copying a std::ifstream is nonsense).

一个简单的方法是添加

private:
    ReadFile(const ReadFile&);
    ReadFile& operator=(const ReadFile&);

在C ++ 11中,使用 = delete 语法。

In C++11, use the = delete syntax.

public:
    ReadFile(const ReadFile&) = delete;
    ReadFile& operator=(const ReadFile&) = delete;

这篇关于无法在头文件中声明ifstream类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 01:33