Compiler says "error: assignment of read-only location"

I have made a program that uses 3 classes that each do a specific task. The base class is used to declare 2 pure virtual functions that are instantiated in the other 2 derived classes.

One derived class uses one virtual function to get an entire copy of a file and store it in a c-string array, and the other derived class uses the second virtual function to "transform" all letters in the file to upper case (and store it in a c-string array). But the compiler says that there is an error "assignment of read-only location".

I have been trying to correct this problem but i can't seem to actually correct it. Any help is appreciated.

P.S the program is not finished


Here is the driver program that tests all the classes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include "Transform class.h"
#include "OriginalFile class.h"

using namespace std;

int main ()
{


    fstream file;
    string name;

    cout << "Please type in a name of a file" << endl;
    cin >> name;

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

}




This is a derived class that is derived from the "FileFilter class"
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
#define TRANSFORM CLASS_H
#include "FileFilter class.h"
#include <fstream>
using namespace std;

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

public:

    virtual char transform (char, fstream &) const;



};

char Transform::transform (char ch, fstream &file) const
{
        int n = 0;
        int m = 0;
        int b = 0;
        char byte;


        while (!file.eof())
        {
            file.get (byte);
            lines[n] = byte;     //assignment of read only location?
            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 another class that is derived from the "FileFilter" class
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
#include <fstream>
#define FILE FILTER_H
#define ORIGINALFILE CLASS_H
#include "FileFilter class.h"
using namespace std;

class OriginalFile : public FileFilter
{
protected:

    char lines_ [1000];


public:

    virtual void readFile (fstream &) const;


};


void OriginalFile::readFile (fstream & file1) const
{
        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] << endl;
            m++;
        }
}





And 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 char transform (char, fstream &) = 0;
    virtual void readFile (fstream &) = 0;

};


Also, the compiler says "error: redefinition of FileFilter class" But i dont see where i have redefined the filefiler class..
Last edited on
But the compiler says that there is an error "assignment of read-only location".
Exact error message please.
looks like you need to guard your headers....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef FILEFILER_CLASS_H            <<<<<<<<<<< add this

#define FILEFILTER CLASS_H
#include <fstream>

using namespace std;

class FileFilter
{
public:

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

};

#endif                  <<<<<<<< and this 


alternatively, if you are using MSVC you can pragma it instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include <fstream>

using namespace std;

class FileFilter
{
public:

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

};


oh, and as you will find, each derived class MUST implement all pure virtuals of the base class, you can't pick and choose.
Last edited on
Topic archived. No new replies allowed.