Why does my program crash everytime i try to call this function?

Every time i try to call a function from a derived class the program keeps crashing. The function is first declared as a pure virtual function, but then it is defined in the derived class and becomes instatiated. But when i call the function, the program just crashes.. what am i doing wrong?


Here is driver program
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
#include <iostream>
#include <fstream>
#include "Transform and original class.h"

using namespace std;

int main ()
{
    OriginalFile *pointer1;

    fstream file;
    string name;

    cout << "Please type in a name of a file" << endl;
    getline (cin, name);

    file.open (name.c_str(), ios::in | ios::out | ios::app);

    if (file.is_open())
        cout << "File has been opened" << endl;

    else
        cout << "Error" << endl;

    pointer1->readFile (file);         //Here is where the program keeps crashing

}


Here are the derived classes
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
72
73
74
75
76
77
78
#define TRANSFORM AND ORIGINAL CLASS_H
#include "FileFilter class.h"
#include <fstream>
#include <iostream>
using namespace std;

//this class will transform the content in the file to all uppercase

class Transform : public FileFilter
{
   char lines [100000];

public:
    virtual void transform (fstream &file)
{
        int n = 0;
        int m = 0;
        int b = 0;
        char byte;

        while (!file.eof())
        {
            file.get(byte);
            lines[n] = byte;
            n++;
        }

        file.seekg (0, ios::beg);

        while (!file.eof())
        {
            lines[m] = toupper(lines[m]);
            m++;
        }

        file.seekg (0, ios::beg);


        while (!file.eof())
        {
            cout << lines[b];
            b++;
        }
    }
};

//This is Class that will hold the original copy of the file
class OriginalFile : public FileFilter
{

    char lines_ [100000];


public:

    virtual void readFile (fstream & file1)
    {
        int n = 0;
        int m = 0;
        char byte;

        while (!file1.eof())
        {
            file1.get(byte);
            lines_[n] = byte;
            n++;
        }

        file1.seekg (0, ios::beg);

         while (!file1.eof())
        {
            cout << lines_[m];
            m++;
        }
    }
};


Here is the base class
1
2
3
4
5
6
7
8
9
10
11
12
13
#define FILEFILTER CLASS_H
#include <fstream>

using namespace std;

class FileFilter
{
public:

    virtual void transform (fstream &) = 0;
    virtual void readFile (fstream &) = 0;

};
Last edited on
You have never made pointer1 point to anything, so it refers to garbage. You then try to call a method on it. See the issue?
I tried to declare a class object from one of the derived classes so that i can assign its address to the pointer, but the compiler is not letting me. Did i instatiate the pure virtual functions properly?
What error do you get when you try to create an object? That should explain enough for you to fix the problem.
Nevermind, I fixed the problem. It turned out I was supposed to define all the pure virtual functions in all derived classes in order for me to create an object.
Topic archived. No new replies allowed.