-4

I'm doing a homework assignment on Templates and reading from files. I have a template class called 'Cfile', and a 'Student' class. I'll put the relevant code here, then ask about the error:

#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

}

And I get the error: cannot convert from 'std::ifstream' to 'char*' on the return line.

The Student class of course has a C'tor that gets an ifstream& in and creates a new Student:

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

EDIT: I think I understand what's wrong now. I have another class called 'MyString', and it has the following read method:

char* read(ifstream& in);

So maybe I've accidentally overridden the read method? But the return type is different, so it shouldn't happen.

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() {};
};
ntrch
  • 76
  • 1
  • 22

1 Answers1

3

I'll try to answer as good as possible given the quality of the question.

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

which in turn fails here

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;


}

You are trying to build a char * out of an ifstream, which fails because those are very different types. I suggest you to rethink the marked line. I also suspect you're not entirely sure what an ifstream actually is, so I advise to re-read the documentation.
On a side node, your call to new on apBuf = new T*[aNum]; doesn't call delete beforehand, which means you have a memory leak

Community
  • 1
  • 1
Jan Hohenheim
  • 3,552
  • 2
  • 17
  • 42