2016-08-12 22 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::Student(ifstream & in) 
{ 
in.read((char*)&age, sizeof(age)); 
} 

EDIT:

コースの学生クラスはifstream& inを取得し、新しい学生を作成C'torを持っている私は今、間違っているかを理解だと思います。 は、私は「のMyString」と呼ばれる別のクラスを持っている、そしてそれは、次のreadメソッドを持っています

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

あなたは最小限と完全な例を提供することができます持っていることを意味これは、事前にdeleteを呼び出すことはありませんか?投稿した内容は完全ではありません。 –

+1

エラーはどの行にありますか?あなたもその行を投稿できますか? – Matthias

+0

私はそれを編集しました。あなたがコードをもっと必要とするなら、downvotingの代わりにそれを求めてください。 – ntrch

答えて

3

私は質問の質を考えれば、できるだけ良い答えを出そうとします。あなたがそれらは非常に異なったタイプであるため失敗しifstream、外のchar *を構築しようとしている

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; 


} 

今度はここで失敗し

Cfile<char*> st; 
    Queue<char*> st1; 
    for (int i = 0;i < 10;i++) 
    { 
     st.read(st1.arr, 10); // Error happens here 
    } 
    i.close(); 

。マークされた行を再考することをお勧めします。また、ifstreamが実際に何であるか完全にはわからないと思われるので、the documentationを再読することをお勧めします。側ノードで
apBuf = new T*[aNum];newへのお電話は、あなたがmemory leak

+0

お返事ありがとうございますが、私の答えはあまり明確ではありません。 apBufは学生へのポインタの配列です。そして、this-> read(); cfileメソッドのreadを使うことになっています。これは、生徒によって構築された生徒を返します。この生徒はfileInをパラメータとして取得します。私は間違っていますか? – ntrch

関連する問題