Class inside another class

Guys, how can I instantiate a class inside another class? I tried the normal way and did not work.

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
class listaSequencial
{
    public:

        // Criar a lista vazia de tamanho n
        bool criarLista(int n)
        {
            int lista[n];
            return true;
        }
        
        // Chamar método para adicionar um elemento
        void adicionar(int elemento)
        {
            l.adicionar(lista,elemento);
        }
        
    private:
        Lista l;
};

class Lista
{
    public:
        // Construtor para inicializacao do atributo qtdElementos
        Lista()
        {
            qtdElementos = 0;
        }
        
        // Adicionar um elemento ao final da lista
        void adicionar(int lista[], int elemento)
        {
            lista[qtdElementos++] = elemento;
        }
    private:
        int qtdElementos;
};



The error message:

19 `Lista' does not name a type
In member function `void listaSequencial::adicionar(int)':
15 `l' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
15 `lista' undeclared (first use this function)
Last edited on
You do not show the error messages. What are they?

If you define List before mytest, does that work?
Ok, now I post the real code and the error message.

In the line 19 I try to inicializate the other class Lista, but the sintax is wrong. What is the correct mode of this declaration?

Thanks.
Last edited on
You need to move class Lista to appear before listaSequencial.
Ok, thanks, it seems that it worked. But now I have another question, typical beginner. I want to create a array, inside the class dynamically. The method criarLista do it to me with int lista[n]; but the function below, void adicionar(int elemento), when I try to use the array created l.adicionar(lista,elemento); returns me an error saying `list 'undeclared (first use this function).

How can I solve this problem?

Thanks again.

The last version of the code:

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
class Lista
{
    public:
        // Construtor para inicializacao do atributo qtdElementos
        Lista()
        {
            qtdElementos = 0;
        }
        
        // Adicionar um elemento ao final da lista
        void adicionar(int lista[], int elemento)
        {
            lista[qtdElementos++] = elemento;
        }
    private:
        int qtdElementos;
};

class listaSequencial
{
    public:
        
        // Criar a lista vazia de tamanho n
        bool criarLista(int n)
        {
            int lista[n];
            return true;
        }
        
        // Chamar método para adicionar um elemento
        void adicionar(int elemento)
        {
            l.adicionar(lista,elemento);
        }
        
    private:
        Lista l;
};

Last edited on
When you say:
1
2
3
4
5
        bool criarLista(int n)
        {
            int lista[n];
            return true;
        }

You're just creating an array of size n and letting the scope destroy it. You need to keep the array as a data member.

Also, watch out for this:

lista[qtdElementos++] = elemento;

Postfix increment returns the previous value, not the incremented value. You also need to make sure you won't write past the end of the array.

PS: eu sugiro programar sempre em inglês; os termos em português só causam confusão e tornam o programa mais difícil de ler.
But I can't declare a array as a data member because I don't know - yet - the array length.
What is the solution? Understand the logic of my program?

A more general question. How to make a list then? I am studying data structure and want to make a list of some common operations such as add, delete and get information and to verify whether the same is empty or full, among others.

But this is very difficult in C++ for me yet ...

Thanks.
Last edited on
Make a pointer and use new to allocate the size.
How???


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
class Lista
{
    public:
        // Construtor para inicializacao do atributo qtdElementos
        Lista()
        {
            qtdElementos = 0;
        }
        
        // Adicionar um elemento ao final da lista
        void adicionar(int lista[], int elemento)
        {
            lista[qtdElementos++] = elemento;
        }
    private:
        int qtdElementos;
};

class listaSequencial
{
    public:
        
        // Criar a lista vazia de tamanho n
        bool criarLista(int n)
        {
            lista(n) = new int();
            return true;
        }
        
        // Chamar método para adicionar um elemento
        void adicionar(int elemento)
        {
            l.adicionar(lista,elemento);
        }
        
    private:
        Lista l;
        int * lista;
};


Use new int[n] instead of just new int. You'll need to delete[] lista in the destructor though.
Yes!!!!!! It works!!!!!!!

Thank you very much!!!!!!
Topic archived. No new replies allowed.