Array of classes and error

Hello my fellow programmers

I'm writing a little program for omnet++ using C++ and I'm having a little error with the code I hope you cand find the solution

in

MyModule.h
1
2
3
4
5
protected:
        int arrSize;
	MyClass* arrClass;
public:
	virtual void addToArray(MyClass* msg);

MyModule.cc
1
2
3
4
5
6
7
8
9
10
void MyModule::addToArray(MyClass* pack) {

    MyClass *var2 = new MyClass[arrSize + 1];
    for (int k = 0; k < arrSize; k++) {
        var2[k] = arrClass[k];
    }
    var2[arrSize] = pack;
    arrSize++;
    arrClass=var2;
}


The error is in "var2[arrSize]=pack" line 7 and I don't find the cause of that error.

var2 is an array of objects or an array of pointers?

This is truly a beginner's question but I can't find the solution

Thank you in advance for your time.

Edit:

The error shown by the compiler is this


no match for 'operator=' in '*(var2 + ((unsigned int)(((unsigned int)((MyModule*)this)->MyModule::arrSize) * 168u))) = pack'
Last edited on
Well, arrClass is never made into an array. It's still a pointer.
The problem is that var2[arrSize] has type MyClass while pack has type MyClass *

So ypu should substitute this statement

var2[arrSize] = pack;

for

var2[arrSize] = *pack;

Also before assigning var2 to arrClass you shall delete the previous allocated memory that is you shall insert statement

delete [] arrClass;
arrClass=var2;

You could substitute the loop

for (int k = 0; k < arrSize; k++) {
var2[k] = arrClass[k];
}


for using of the standard algorithm std::copy declared in header <algorithm>

std:;copy( arrClass, arrClass + arrSize, var2 );
Last edited on
Thanks for your answers.
Topic archived. No new replies allowed.