所以我在处理文件ofstream和ifstream时陷入了一个编译器问题,我只是根本不知道这意味着什么...
开始吧。
我有以下课程:
class FS{
public:
string name;
long long int size;
long long int freeBlocks;
long long int usedBlocks = 0;
int blocksize = 128;
vector<FS_File> file_list;
char * ptr; //This is a malloc pointer
void saveTo(ofstream& of);
void openFrom(ifstream& inf);
};
saveTo() 函数中出现问题:
void FS::saveTo(ofstream& of){
of.write(&name, sizeof(name));
of.write(&size, sizeof(size));
of.write(&freeBlocks, sizeof(freeBlocks));
of.write(&usedBlocks, sizeof(usedBlocks));
of.write(&blocksize, sizeof(blocksize));
of.write(&file_list, sizeof(file_list));
of.write((char *)&ptr, sizeof(ptr));
}
编译器给我下一个错误:
functions.cpp In member function 'void FS::saveTo(std::ofstream&)':
[Error] no matching function for call to 'std::basic_ofstream<char>::write(std::string*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ofstream<char>::write(long long int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ofstream<char>::write(long long int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ofstream<char>::write(long long int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ofstream<char>::write(int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ofstream<char>::write(std::vector<FS_File>*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ifstream<char>::read(std::string*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ifstream<char>::read(long long int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ifstream<char>::read(long long int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ifstream<char>::read(long long int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ifstream<char>::read(int*, long long unsigned int)'
[Error] no matching function for call to 'std::basic_ifstream<char>::read(std::vector<FS_File>*, long long unsigned int)'
每当我尝试调用下一个代码时:
ofstream outfile;
string filename = curFS.name + ".dat";
outfile.open(filename, ios::binary | ios::out);
curFS.save(outfile);
outfile.close();
我尝试了几件事,但是没有任何效果...
编译器错误是什么意思? 我该如何解决?
答案 0 :(得分:1)
错误消息不言自明。 std::ifstream
和std::ofstream
是char
数据流,但是您正在尝试使用指向非char
类型的指针来读取/写入数据。非write()
指针的read()
和char
方法没有重载。
对于POD类型,您只需键入将指针指向char*
的类型。
这不适用于非POD类型,例如std::string
和std::vector
。对于这些类型,您需要
请尝试以下类似操作:
void FS::saveTo(ofstream& of)
{
// std::string needs to be marshaled...
size_t value = name.size();
of.write(reinterpret_cast<char*>(&value), sizeof(value));
of.write(name.c_str(), value);
// POD types can be written as-is...
of.write(reinterpret_cast<char*>(&size), sizeof(size));
of.write(reinterpret_cast<char*>(&freeBlocks), sizeof(freeBlocks));
of.write(reinterpret_cast<char*>(&usedBlocks), sizeof(usedBlocks));
of.write(reinterpret_cast<char*>(&blocksize), sizeof(blocksize));
// std::vector needs to be marshaled...
value = file_list.size();
of.write(reinterpret_cast<char*>(&value), sizeof(value));
for (size_t i = 0; i < value; ++i) {
// write file_list[i] to the stream as needed...
}
// dynamic data needs to be marshaled...
value = strlen(ptr); // or whatever the actual allocated size is...
of.write(reinterpret_cast<char*>(&value), sizeof(value));
of.write(ptr, value);
}
void FS::openFrom(ifstream& inf)
{
size_t value;
// std::string needs to be marshaled...
inf.read(reinterpret_cast<char*>(&value), sizeof(value));
name.resize(value);
inf.read(&name[0], value);
// POD types can be read as-is...
inf.read(reinterpret_cast<char*>(&size), sizeof(size));
inf.read(reinterpret_cast<char*>(&freeBlocks), sizeof(freeBlocks));
inf.read(reinterpret_cast<char*>(&usedBlocks), sizeof(usedBlocks));
inf.read(reinterpret_cast<char*>(&blocksize), sizeof(blocksize));
// std::vector needs to be marshaled...
inf.read(reinterpret_cast<char*>(&value), sizeof(value));
file_list.resize(value);
for (size_t i = 0; i < value; ++i) {
// read file_list[i] from stream as needed...
}
// dynamic data needs to be marshaled...
inf.read(reinterpret_cast<char*>(&value), sizeof(value));
free(ptr);
ptr = (char*) malloc(value+1);
inf.read(ptr, value);
ptr[value] = '\0';
}
只需确保始终在启用std::ios_base::binary
标志的情况下打开流。为了确保这一点,我建议将文件名而不是用户提供的流传递给saveTo()
/ openFrom()
:
void FS::saveTo(const string& filename)
{
ofstream outfile(filename, ios::binary);
saveTo(outfile);
}
void FS::openFrom(const string& filename)
{
ifstream infile(filename, ios::binary);
openFrom(infile);
}
curFS.saveTo(curFS.name + ".dat");
curFS.openFrom(curFS.name + ".dat");