error C2248,这是什么错误,我该如何解决?

error C2248, what is this error and how do I fix it?

我是 c++ 的新手,目前正在尝试学习模板及其工作原理。我的作业要求我创建一个父 class 基础,它将 4 个参数带入模板。它还必须有一个 subclass 可以打印基数 class.

的值

父 class 通过读取 txt 文件获取值。

现在我正在尝试实例化一个子class,以便我可以在父 class.

中逐行打印值

我的代码很乱,因为我对 C++ 还很陌生。我的背景包括 C#、Java 和 Javascript。任何帮助都会很棒!提前致谢。

错误信息:

Error   1   error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>'

代码:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cctype>

using namespace std;

//template that accept 4 different data types
template<class Type1, class Type2, class Type3, class Type4>
class Base {
    //protected declarations
    protected:
        Type1 str;
        Type2 i;
        Type3 d;
        Type4 b;

    //public declarations
    public:
        int lineRead;
        int data;
        string line, delimiter;
        size_t pos;
        ifstream inputfile;
        string token;

    //public functions
    public:
        void readFile(int lineNum) {
            //temp information holders
            lineRead = 0;
            pos = 0;
            delimiter = " ";

            //open .txt file
            inputfile.open("file.txt");

            //while end of file is not reached
            while (!inputfile.eof()) {
                //increment line counter
                lineRead++;

                //read line
                getline(inputfile,line);

                //if this is the line requested fill the template members with the
                //correct data. <string, int, double, bool>
                if(lineNum == lineRead){
                    //getting the string
                    pos = line.find(delimiter);
                    token = line.substr(0, pos);
                    str = token;
                    line.erase(0, pos + delimiter.length());

                    //getting the integer
                    pos = line.find(delimiter);
                    token = line.substr(0, pos);
                    i = stoi(token);
                    line.erase(0, pos + delimiter.length());

                    //getting the double
                    pos = line.find(delimiter);
                    token = line.substr(0, pos);
                    d = stod(token);
                    line.erase(0, pos + delimiter.length());

                    //getting the boolean
                    pos = line.find(delimiter);
                    token = line.substr(0, pos);
                    b = to_bool(token);
                    line.erase(0, pos + delimiter.length());
                }
            }

            //close the file
            inputfile.close();
            system("pause");
        };

        //Changes a string to lower case and then reads the string
        //to find if the value is true or false. Returns a boolean.
        bool to_bool(string str) {
            transform(str.begin(), str.end(), str.begin(), ::tolower);
            istringstream is(str);
            bool tempB;
            is >> boolalpha >> tempB;
            return tempB;
        }
};

class Subclass : public Base<string, int, double, bool> {
    private:
        string name;

    public:
        Subclass::Subclass(string name) {
            cout << "test";
        }

        void printValues() {
            cout << str;
        }


};

//main function
int main() {
    //call Base class and give the template data types
    Subclass b = Subclass("test");

    //read the file
    b.readFile(2);

    return 0;
}

您有一个 std::ifstream 作为成员,但它们是不可复制的,因此您引用了错误消息。在 C++11 之前,使某些内容不可复制的惯用方法是使用私有访问说明符(这就是您所看到的)来声明其复制构造函数,这样就不会有人意外使用它。在 C++11 中,delete 关键字是首选,您会收到类似 "use of deleted function".

的错误

您应该:

  1. 定义复制构造函数和复制赋值运算符
  2. 没有不可复制的成员

如果我是你,我会选择 (2);问问自己为什么需要这个作为会员,然后想办法解决这个问题。

Subclass b = Subclass("test");

这会创建一个对象,然后尝试复制它。您的 class 不可复制,因此会出现错误(另外,复制派生的 classes 是 problematic 以其自己的方式)。要在不复制的情况下创建对象,请使用以下语法:

Subclass b("test");