简体   繁体   English

私有std :: fstream成员无法访问?

[英]Private std::fstream member unable to be accessed?

Why is it unable to be accessed? 为什么无法访问? I cannot understand this error at all. 我完全无法理解此错误。 It's extremely vague and poorly explained by the compiler. 编译器非常模糊且解释不清。

Error   1   error C2248: 'std::basic_fstream<_Elem,_Traits>::basic_fstream' : cannot 
access private member declared in class 'std::basic_fstream<_Elem,_Traits>' c:\users
\user\documents\visual studio 2012\projects\queue\queue\main.cpp    69  1   queue

Line 69 contains nothing but a closing brace with a semicolon. 第69行仅包含带有分号的右括号。

class queue {
public:
    queue() {
        back = NULL;
        front = NULL;
    }
    void setfile(const char* to) {
        file.open(to, std::fstream::in | std::fstream::out | std::fstream::app);
    }
    void push(const char* msg) {
        file << msg << QueryPerformanceCounter(&nano::end);
        if(front == NULL) {
            back = new item(msg);
            front = back;
        }
        else {
            back->setprev(new item(msg));
            back = back->getprev();
        }
    }
    void* pop(const char* msg) {
        file << msg << QueryPerformanceCounter(&nano::end);
        if(front == NULL) {
            return false;
        }
        else {
            item* temp = front;
            front = front->getprev();
            delete temp;
        }
    }
private:
    std::fstream file;
    item* back;
    item* front;
};

That final brace at the end of the class definition is where the error points me to. 在类定义末尾的最后一个括号是错误指出我的地方。 You might be wondering if I've tried to assign or copy the private std::fstream object, but I haven't. 您可能想知道我是否尝试分配或复制私有std :: fstream对象,但没有这样做。 All the code using it is inline within the class definition. 所有使用它的代码都在类定义内联。 The setfile() function is the only code that interacts with it from the outside, and the constant character argument it takes is just the filename. setfile()函数是唯一从外部与之交互的代码,它接受的常量字符参数只是文件名。

Using MSVC++ 2012 at the moment. 目前正在使用MSVC ++ 2012。

std::fstream has not public copy constructors or assignment operator. std :: fstream没有公共副本构造函数或赋值运算符。 So implicit generated copy constructor for queue class can not copy of file object. 因此,为队列类隐式生成的副本构造函数无法复制文件对象。

First, you can deny copying of queue object by explicit declaration of private copy constructor. 首先,您可以通过显式声明私有副本构造函数来拒绝复制队列对象。 Second solution is to include your fstream by pointer not by value. 第二种解决方案是按指针而不是按值包含fstream。 To prevent memory problems, use shared_ptr instead of simple pointers. 为了防止出现内存问题,请使用shared_ptr而不是简单的指针。

UPDATE I 更新

f no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as an inline public member of its class. 如果没有为类类型(结构,类或联合)提供用户定义的副本构造函数,则编译器将始终将副本构造函数声明为其类的内联公共成员。

From cpp reference 来自cpp参考

So even if you doesn't copy your queue objects, compiler will generate copy constructor anyway. 因此,即使您不复制队列对象,编译器仍然会生成复制构造函数。 And this constructor cannot copy std::fstream object. 而且此构造函数无法复制std :: fstream对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM