2016-08-12 415 views
-5

我正在模板上做家庭作业,并从文件中读取数据。 我有一个名为'Cfile'的模板类和'Student'类。我会在这里把相关的代码,然后询问有关该错误:C++:无法从'std :: ifstream'转换为'char *'

#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& in并创建一个新的学生:

Student::Student(ifstream & in) 
{ 
in.read((char*)&age, sizeof(age)); 
} 

编辑:我想我明白现在是错误的。 我有一个名为“MyString的”另一个类,它具有以下读法:

char* read(ifstream& in); 

所以也许我不小心覆盖read方法?但是返回类型是不同的,所以它不应该发生。

EDIT#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() {}; 
}; 
+1

你能提供一个最小的,完整的例子吗?你发布的内容不完整。 –

+1

哪一行是错误?你也可以发布该行吗? – Matthias

+0

我编辑它,如果你需要更多的代码请求它,而不是downvoting。 – ntrch

回答

3

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

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究竟是什么,所以我建议重新阅读the documentation
在一个侧面节点,你到newapBuf = new T*[aNum];调用不会调用delete事前,这意味着你有一个memory leak

+0

谢谢你试图回答,但你的回答不是很清楚。 apBuf是一个指向学生的数组。并且this-> read();应该使用cfile方法读取,它返回由学生c'tor构建的学生获取fileIn作为参数。我错在哪里? – ntrch

相关问题