0
class RLE_v1
{
    struct header
    {
        char sig[4];
        int fileSize;
        unsigned char fileNameLength;
        std::string fileName;
    } m_Header;

    RLE<char> m_Data;

public:
    void CreateArchive(const std::string& source)
    {
        std::ifstream::pos_type size;
        char* memblock;
        std::ifstream file (source, std::ios::in|std::ios::binary|std::ios::ate);
        if (file.is_open())
        {
            size = file.tellg();
            memblock = new char [static_cast<unsigned int>(size)];
            file.seekg (0, std::ios::beg);
            file.read (memblock, size);
            file.close();
            //
            // trying to make assignment to m_Data here.
            //
            delete[] memblock;
        }
    }

    void ExtractArchive(const std::string& source);
};

I'm trying to copy the data in the "memblock" char array into the variable m_Data, but when I try to do it I get the error that no match for operator "=" matches these operands. I have no idea how to make them equal, because m_Data is already of type char.

This is the RLE class that has the variable m_Data as a mem

template <typename T>
struct RLE
{

    T* m_Data;  // Memory which stores either compressed or decompressed data
    int m_Size; // Number of elements of type T that data is pointing to

    RLE()
        : m_Data(nullptr)
        , m_Size(0)
    { }

    ~RLE()
    {
        delete m_Data;
    }
};
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
user1665569
  • 191
  • 4
  • 8
  • 17

1 Answers1

1

You've shown everything except the actual line that produces the error.

But what I see is this. You have a class that has the member:

RLE<char> m_Data;

After template expansion, that struct will have the member:

char *m_Data;

You say there is no operator= when you assign memblock to m_Data. So I can only conclude that you are doing this:

m_Data = memblock;

Where you should actually be doing this:

m_Data.m_Data = memblock;
m_Data.m_Size = size;

Instead of directly operating on a struct's internals, you might be better off making a function:

template <typename T>
void RLE<T>::Alloc( size_t size )
{
    if( m_Data != nullptr ) delete [] m_Data;
    m_Data = new T [size];
    m_Size = size;
}
paddy
  • 60,864
  • 6
  • 61
  • 103
  • why should it be m.Data.m_Data? sorry i'm a little bit confused – user1665569 Feb 12 '13 at 03:20
  • and yes i was trying to do m_data = memblock; and the line that produces the error is where i said "i tried to copy the data..." sorry. – user1665569 Feb 12 '13 at 03:21
  • 1
    +1 I'm guessing saving off that pointer into the `RLE` is probably going to be worthless if it is followed up immediately by `delete[] memblock`. And following up your Alloc suggestion, even more home: `RLE::ReadStream(ifstream& inf, size_t size)` may equally come in handy. – WhozCraig Feb 12 '13 at 03:21
  • Haha good point @WhozCraig - Hope you're listening, user1665569. After you copy a pointer with the intention of passing ownership to someone else, don't then go and delete it. Copying pointers doesn't duplicate the memory. If you delete your memory, all pointers to that memory become worthless. – paddy Feb 12 '13 at 03:26
  • once i fill up memblock i'm trying to store it into the variable m_data so i can use m-Data it for compression or decompression... – user1665569 Feb 12 '13 at 03:26
  • @user1665569 Your problem is more to do with your choice of variable names. If you hadn't made an instance of `RLE` called `m_Data` which contains a `char*` variable called `m_Data` you wouldn't be half as confused as you are now. – paddy Feb 12 '13 at 03:26
  • 1
    @user1665569 paddy has both the right answer, and some good suggestions, and i'll leave this little tryst with one more: use a [`std::vector<>`](http://en.cppreference.com/w/cpp/container/vector) for this, and a LOT of problems go away. – WhozCraig Feb 12 '13 at 03:29
  • OOH..thank you @paddy. This is not my code. The teacher gave it to us as a lab, and I got really lost on that part. – user1665569 Feb 12 '13 at 03:30
  • thank you both @WhozCraig and paddy. you two have been truly useful – user1665569 Feb 12 '13 at 03:31