我正在使用 ifstream 从文件中获取输入,如下面的代码所示。
我知道您不能直接获取 ifstream,因为它是私有(private)成员。通过引用获取它的工作是什么?语法是什么样的?
下面是我的 B.cpp 和 B.h,我认为没有必要附加其他文件,但如果需要可以附加。另外,我在下面附上了错误消息!
提前感谢您的帮助!
B.h:
#ifndef B_H
#define B_H
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <iostream>
class B {
public:
//Variable declarations
std::string line, delimiter, token, str;
std::ifstream inputfile;
size_t pos;
int lineRead;
//Function declarations
B();
void setValues(int lineNum);
std::string printValues();
bool to_bool(std::string str);
protected:
float f;
int i;
bool b;
std::string s;
};
#endif
B.cpp:
#include "B.h"
B::B() {
}
void B::setValues(int lineNum) {
lineRead = 0;
pos = 0;
delimiter = " ";
inputfile.open("file.txt");
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, float, 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 float
pos = line.find(delimiter);
token = line.substr(0, pos);
f = stof(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();
}
std::string B::printValues() {
return s + " " + std::to_string(i) + " " + std::to_string(f) + " " + std::to_string(b) + "\n";
}
//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(std::string str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
std::istringstream is(str);
bool tempB;
is >> std::boolalpha >> tempB;
return tempB;
}
S.h:
#ifndef S_H
#define S_H
#include "B.h" // required to make B known here
#include <string> // known through B, but better safe than sorry
class S : public B {
public:
S(std::string name);
std::string subPrint(); // ***
protected:
std::string s2;
};
#endif
S.cpp:
#include "S.h"
S::S(std::string name) : s2(name) {
}
std::string S::subPrint () {
return s2 + " " + printValues();
}
主要.cpp:
#include "S.h"
#include <vector>
using namespace std;
int main() {
int itPos = 0;
vector<S> v;
S s1("Jon");
S s2("Mike");
S s3("Kim");
S s4("Steve");
S s5("Kevin");
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
v.push_back(s5);
cout << v[0].subPrint();
system("pause");
return 0;
};
最佳答案
你在这里的问题是 std::ifstream
是不可复制的。这意味着 B
无法复制,反过来 S
也无法复制。当您将 S
添加到 vector 中时
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
v.push_back(s5);
vector 尝试复制传递给它的内容以将其放置在 vector 中。
解决这个问题的一个简单方法是从类成员中删除 ifstream
,只将它作为 setValues
中的局部变量,因为这是唯一使用它的地方.
关于c++ - 在这种情况下,您如何正确使用 ifstream 的引用?错误 C2248,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36701408/