0

I'm trying to create an array of objects within another object and decide the magnitude of the array.

Why I get an error when I try to assign "obj2T" to "obj2"?

Pastebin code link: https://pastebin.com/kujujP5N

What is the correct syntax for creating an array of objects within another object and decide the magnitude of the array?

#include <iostream>

using namespace std;

class classe2
{
    public:
        classe2();

    protected:

    private:
};

class classe1
{
    public:
        classe1(int value);
        void setClasse()
        {
        classe2 obj2T[grandezza];
        obj2=obj2T;
        }

    protected:

    private:
        const int grandezza;
        classe2 obj2[];
};

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

Error:

C:\cppProjects\project\main.cpp||In member function 'void classe1::setClasse()'
C:\cppProjects\project\main.cpp|22|error: incompatible types in assignment of 'classe2 [((classe1*)this)->classe1::grandezza]' to 'classe2 [0]'
nostyn
  • 19
  • 1
  • 6

1 Answers1

1

The correct syntax is

#include <vector>

...

class classe1
{
    public:
        classe1(int value) :
            obj2 (value)
        {
        }
    private:
        std::vector<classe2> obj2;
};
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243