Creating a Mimic Class

I have two classes that are functionally identical, but which I want to make separate so that the same class of object can be used in different constructors for another class. I decided to make my second class inherit from the first.

Here's my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef INFOREADER_H
#define INFOREADER_H

#include <iostream>
#include "ArrayList.h"

using namespace std;

class TextReader //interface for reading the passwd or group file
{
public:
    ArrayList<char> getText() //reads from a file
    {
        ArrayList<char> output = ArrayList<char>(256);

        for(int i = 0; i < 256; i++)
            output.append('\0');
		
        while(!(in.eof()))
            in.get();

        int i = 0;

        while(in.get() != '\0')
        {
            output.insert(in.get(), i);
            in.get();
            ++i;
        }

        return output;
    }

    ArrayList<ArrayList<char> > parseText(ArrayList<char> block) //formats info from file
    {
        ArrayList<char> temp = ArrayList<char>();
        ArrayList<ArrayList<char> > turn = ArrayList<ArrayList<char> >();
		
        for(int i = 0; i < block.getSize(); i++)
        {
            if(block[i] == '\n')
                ++i;
            else
            {
                while(block[i] != '\n')
                    temp.append(block[i]);

                turn.append(temp);
                temp = ArrayList<char>();
            }
        }

        return turn;
    }
    TextReader(istream& i): in(i)
    {}
    virtual ~TextReader()
    {}

protected:
    istream& in;
};

class InputReader: public TextReader //for reading P1Input
{
public:
    InputReader(istream& i): in(i) //I'd make 'i' const but that annoyed the compiler
    {} //apparently, 'in' is not a member, so it didn't get initialized
    virtual ~InputReader()
    {}
};


The second class' constructor is giving me a "No default constructor" error when I compile even though it's exactly like the first one.

I tried assigning in inside the brackets, but that didn't work since the istream's operator= is private.

Any insight available?
http://en.cppreference.com/w/cpp/language/initializer_list (initialization order)
To construct the derived class you need to construct the base class first. However, you do not have any call to a base class constructor in your initialization list.
The compiler guess that you want to use the default constructor for the base class, but in your case you do not have a default constructor, so it fails.

1
2
InputReader(std::istream &i): TextReader(i)
{}



> so that the same class of object can be used in different constructors for another class.
¿care to expand?
The classes function the same way but they're intended for different types of files. One is for save files while the other is for a sequence of scripts. Don't ask why I'm not using <fstream>.
Topic archived. No new replies allowed.