C ++:无法从“ std :: ifstream”转换为“ char *”

ntrch

我正在做关于模板的作业,并从文件中读取。我有一个名为“ Cfile”的模板类和一个“学生”类。我将相关代码放在这里,然后询问错误:

#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”的类,它具有以下读取方法:

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() {};
};
扬·尼尔斯·费纳(Jan Nils Ferner)

考虑到问题的质量,我将尽力回答。

   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;


}

您正在尝试从中构建char *出一个ifstream,但失败的原因是,这是非常不同的类型。我建议您重新考虑标线。我还怀疑您不确定确切的含义ifstream,因此建议您重新阅读文档
在侧节点上,您对newon的调用apBuf = new T*[aNum];不会delete事先调用,这意味着您存在内存泄漏

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++错误:无法将'std :: basic_string <char>'转换为'const char *'

错误无法将赋值中的'std :: string {aka std :: basic_string <char>}'转换为'char'-C ++

从std :: vector <int>转换为char [] C ++

错误C2440'<函数样式-广播>':无法从'char'转换为'std :: string'

C2440:“ =”:无法从“ const char [9]”转换为“ char *”

错误:无法从'std :: string * {aka std :: basic_string <char> *}'转换为'std :: string {aka std :: basic_string <char>}'||

将未签名的char *转换为std :: istream * C ++

C++ 将 std::string 转换为 char

C ++ const char *转换为std :: string&

无法将参数'2'的'std :: string {aka std :: basic_string <char>}'转换为'char *'到'int Save(int,char *)'

错误:无法将'std :: basic_string <char> :: iterator ...'转换为'const char * for parameter'1'...'

错误无法将“coll”(类型“std::__cxx11::list<char>”)转换为“const char&”

错误C2440:'正在初始化':无法从'初始化列表'转换为'std :: vector <char *,std :: allocator <_Ty >>'

错误无法在赋值中将 'std::string* {aka std::basic_string<char>*}' 转换为 'node*'

std :: string转换为char *

将std :: vector <char>转换为std :: vector <T>转换c ++

C++:无法将字符串转换为 char*?

错误 C2440:“=”:无法从“const char *”转换为“LPCWSTR”

在C#中将int转换为char无法正常工作

Unity / Objective-C:无法将char *转换为NSString

错误:初始化时无法将'std :: string {aka std :: basic_string <char>}'转换为'char *'

无法在C中使用循环而无法将char *指针转换为小写/大写char *指针

如何将 std::vector<std::string> 转换为 C api 的 char*[]

C / C ++ strcmp无法将参数1从'char'转换为'const char *'

无法从char []转换为int []

无法从void转换为char

无法从String转换为Char

无法从“char[]”转换为“string[]”

无法从const char *转换为const char *&