C++ 对';std::流的基本单元<;char>;::写入(std::string*,long-long-unsigned-int)和#x27;
所以我在处理流文件和ifstream时遇到了一个编译器问题,我只是不知道这意味着什么 让我们开始吧 我有以下课程:C++ 对';std::流的基本单元<;char>;::写入(std::string*,long-long-unsigned-int)和#x27;,c++,c++11,pointers,malloc,ofstream,C++,C++11,Pointers,Malloc,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
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);
};
编译器给我下一个错误:
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)'
我试过一些方法,但都不管用
编译器错误意味着什么?
如何解决此问题?错误消息是不言自明的
std::ifstream
和std::ofstream
是char
数据流,但您正试图使用指向非char
类型的指针读取/写入数据。非char
指针的write()
和read()
方法没有重载
对于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';
}
of.write(&name,sizeof(name))代码>的作用与您想象的不同。按地址将std::string
写入文件流最终只会导致无意义的结果。按地址编写std::vector
对象也不是好兆头。按地址写char*
也不会。只要想想在所有这些情况下,磁盘文件中实际写入了什么。而且,错误消息的意思正是他们所说的。请看合适的原型。您的by地址写入中有多少与该类型匹配。请记住,C++是强类型的。这确实是我的问题的解决方案,但有一个小问题。首先,我有一个问题。。。该sol是否读取openFrom函数中的信息并将其加载回类中?如果有:假设我创建了.dat文件并关闭了我用来创建该文件的程序。现在我打开另一个实例并告诉它打开.dat文件。它确实读取了文件,但当我试图访问它时,它根本没有任何信息。如果没有,我如何将信息加载回?对不起,我问了这么多问题,但这确实是我第一次使用它。@AlanMaldonado“这个sol是否读取openFrom函数中的信息并将其加载回类?”-是的。“它确实读取了文件,但当我试图访问它时,它根本没有任何信息”-那么您要么没有正确加载数据,要么调用了错误的FS
对象上的openFrom()
,或者调用了无效对象。请编辑您的问题,以包括一个不适合您的问题。
ofstream outfile;
string filename = curFS.name + ".dat";
outfile.open(filename, ios::binary | ios::out);
curFS.save(outfile);
outfile.close();
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';
}
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");