【问题标题】:C++: cannot convert from 'std::ifstream' to 'char*'C++:无法从 'std::ifstream' 转换为 'char*'
【发布时间】:2016-08-12 06:36:33
【问题描述】:

我正在做关于模板和读取文件的家庭作业。 我有一个名为“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*' 在返回线上。

Student 类当然有一个 C'tor,它获得 ifstream&amp; in 并创建一个新 Student:

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()
{
}

学生.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() {};
};

【问题讨论】:

  • 您能否提供一个最小且完整的示例?您发布的内容不完整。
  • 在哪一行发现错误?你也可以发布那条线吗?
  • 不,如果您提供让我们重现问题的 MINIMAL 示例将会很有帮助。简单地粘贴您拥有的所有来源是没有帮助的!

标签: c++ file templates


【解决方案1】:

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

   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 到底是什么,所以我建议您重新阅读 the documentation
在侧节点上,您在 apBuf = new T*[aNum]; 上对 new 的调用不会事先调用 delete,这意味着您有一个 memory leak

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2011-08-23
    • 2012-12-11
    • 1970-01-01
    相关资源
    最近更新 更多