C++:无法从'std::ifstream'转换为'char*'
C++: cannot convert from 'std::ifstream' to 'char*'
我正在做关于模板和从文件中读取的家庭作业。我有一个名为"Cfile"的模板类和一个"Student"类。我将把相关代码放在这里,然后询问错误:
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
class Cfile
{
ifstream fileIn;
...jump down a bit
T read()
{
return T(this->fileIn); //the error is in this line
}
我得到了错误:返回行上的cannot convert from 'std::ifstream' to 'char*'
。
学生类当然有一个C'tor获得一个ifstream& in
并创建一个新的学生:
Student::Student(ifstream & in)
{
in.read((char*)&age, sizeof(age));
}
编辑:我想我现在明白是怎么回事了。我有另一个叫做'MyString'的类,它有下面的read方法:
char* read(ifstream& in);
所以也许我不小心重写了read方法?但是返回类型是不同的,所以它不应该发生。
编辑# 2:Main.cpp -
#include"Cfile.h"
#include"Student.h"
#include"Queue.h"
#include "C.h"
int main()
{
ifstream i;
i.open("strings.txt");
Cfile<Student>stuFile("strings.txt");
Student a, b, c;
a.age = 10;
b.age = 20;
c.age = 30;
stuFile.write(a);
stuFile.write(b);
stuFile.write(c);
Student d = Student(i);
Student e = Student(i);
Student f = Student(i);
Cfile<char*> st;
Queue<char*> st1;
for (int i = 0;i < 10;i++)
{
st.read(st1.arr, 10);
}
i.close();
return 0;
}
MyString.cpp -
#include "MyString.h"
MyString::MyString()
{
}
MyString::MyString(char * st)
{
this->st = st;
}
char * MyString::read(ifstream& in)
{
in.read(this->st, sizeof(st));
return st;
}
const char * MyString::getStr()
{
return this->st;
}
MyString::~MyString()
{
}
Student.cpp -
#include "Student.h"
/*bool Student::operator==(Student student)
{
return this->name == student.name &&
this->lastName == student.lastName &&
this->age == student.age;
}*/
Student::Student()
{
}
Student Student::read(ifstream& in)
{
in.read((char*)&age, sizeof(age));
return *this;
}
void Student::write(ofstream & out)
{
out.write((char*)&age, sizeof(age));
}
Student::Student(ifstream & in)
{
in.read((char*)&age, sizeof(age));
}
Student::Student(Student &s)
{
this->age = s.age;
}
Student::Student(int age)
{
//Student s = new Student;
this->age = age;
}
Student::~Student()
{
}
Cfile.h -
#pragma once
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
class Cfile
{
ifstream fileIn;
ofstream fileOut;
public:
Cfile() {};
Cfile(char* _fileName) {
fileIn.open(_fileName, ios::binary);
fileOut.open(_fileName, ios::binary);
}
int read(T **apBuf, int aNum)
{
int count=0;
apBuf = new T*[aNum];
for (int i = 0;i < aNum;i++)
{
*apBuf[i] = this->read();
count++;
}
return count;
}
void write(T &t)
{
t.write(this->fileOut);
}
void write(T* apBuf, int aNum) {
for (int i = 0;i < aNum;i++)
{
write(apBuf[i]);
}
}
int size()
{
fileIn.seekg(0, ios::end);
return (fileIn.tellg() / sizeof(T));
}
T read()
{
return T(this->fileIn);
}
~Cfile() {};
};
考虑到问题的质量,我会尽量回答得好。
Cfile<char*> st;
Queue<char*> st1;
for (int i = 0;i < 10;i++)
{
st.read(st1.arr, 10); // Error happens here
}
i.close();
在这里又失败了
int read(T **apBuf, int aNum)
{
int count=0;
apBuf = new T*[aNum];
for (int i = 0;i < aNum;i++)
{
*apBuf[i] = this->read(); // Error
count++;
}
return count;
}
您正在尝试从ifstream
构建char *
,这失败了,因为它们是非常不同的类型。我建议你重新考虑一下标线。我也怀疑你不完全确定ifstream
实际上是什么,所以我建议你重新阅读文档。
在侧节点上,对apBuf = new T*[aNum];
上的new
的调用不会事先调用delete
,这意味着您有内存泄漏
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 使用char类型将decimal转换为string,将string转换为decimal
- 如何在对<char>C++程序进行逆向工程的同时将 std::basic_string 转换为 Rust 可读值?
- 将函数参数"const char*"转换为"std::string_view"是
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如何在不强制转换每个参数的情况下删除初始值设定项列表中从 int 到 char 的缩小转换?
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 如何使用多次 memcpy 转换回 std::vector<unsigned char>?
- 将"uint8_t"(从套接字读取)隐式转换为"char"安全吗
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- 转换char*时的值不同
- Boost Python 不会自动转换 char* 数据成员
- 除了转换char*之外,还有更好的方法来获得一个变量值吗?
- 如何跨平台转换char*到long long(64位)
- 用atof转换char数组中的浮点数
- C转换char[]为时间戳;