Class with a vector of obects as an attribute

Hey there, I'm having the same problem as the guy in this topic:
http://www.cplusplus.com/forum/general/23091/

Though, my vector isn't one of int, it is a vector of objects from another class and NetBeans won't compile it.
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
#include <cstdlib>
#include<vector>
#include <string>
#include<iostream>

using namespace std;

class estoque{
    string nome_estoque;
    vector<produto> lista_produtos;
public:
    estoque(string nome_estoque_param){
        nome_estoque=nome_estoque_param;
    }
    void add_produto(produto produto_param){
        lista_produtos.push_back(produto_param);
    }
};

class produto{
    int cod_produto,quantidade;
    string nome_produto,unidade;
    vector<preco> lista_precos;
public:
    produto(){}
    produto(int cod_prod_param,int qnt_param,string unid_param,string nome_prod_param){
        cod_produto=cod_prod_param;
        quantidade=qnt_param;
        unidade=unid_param;
        nome_produto=nome_prod_param;
    }
    void add_preco(preco preco_param){
        lista_precos.push_back(preco_param);
    }
};

class preco{
    int valor_preco;
    int data;
public:
    preco(){}
    preco(int a,int b){
        valor_preco=a;
        data=b;
    }
            
};
Okay the following:
estoque doesn't know anything about a class called "produto"
and produtor doesn't know anything about a class called "preco"

This is not java , this is c++

If you reverse the order in which you create the classes it works.

since your compiler doesn't look for the class "produto" below "estoque".
For functions you can do "Forward declaration" for this.

I don't think that works with classes so you have to declare them in the right order.

1
2
3
class preco {}; //this first
class produto {}; // now this
class estoque {}; //aaand finally this one 
Last edited on
You can forward declare classes class produto;
That would work if you have pointers or references. (because of how std::vector is implemented, std::vector<preco> would compile with only a forward declaration).

However, if you pretend to create an object or make use of members, then the definition must be available prior to that point.


> NetBeans won't compile it.
¿why didn't you post the error messages?
1
2
3
class preco {}; //this first
class produto {}; // now this
class estoque {}; //aaand finally this one  


Yeah, this solved the problem, thanks a lot guys. Can't belive it was such a simple thing such as the order. lol
Topic archived. No new replies allowed.