Store objec to vector in another class

I create the object Inicia in main function and have there a vector<frete> fretes and I want to store there frete objects but they are created in another function.

when compiling I get the error that i is a undeclared identifier... can anyone help

I have the following method in class inicia to add objects:

void Inicia::add_frete( const Frete &frete )
{
fretes.push_back( frete );
}

and in another class I have this method to receive the values and create the object:

void Funcionario::registo_frete(){

int continuar;

fstream ficheiro( "fretes.txt", ios::app );

if( !ficheiro)
{
cerr << endl;
cerr << "Erro ao abrir o ficheiro!" << endl;
exit( 1 );
}

do
{
cout << "Registo de Frete" << endl << endl;

string origem_temp = Interface::ler_string( "Introduza a origem: " );
string destino_temp = Interface::ler_string( "Introduza o destino: " );
string cliente_temp = Interface::ler_string( "Introduza o cliente: " );
int kms_temp = Interface::ler_int( "Introduza o numero de KM's: " );
int consumo_temp = Interface::ler_int( "Introduza o consumo de combustivel: " );
int portagens_temp = Interface::ler_int( "Introduza o valor de portagens: " );
int num_veiculo_temp = Interface::ler_int( "Introduza o numero do veiculo: " );
int preco_temp = Interface::ler_int( "Introduza o preco do frete: " );
cout << endl << endl;

ficheiro<< origem_temp<< ";"<< destino_temp<< ";"<< cliente_temp<< ";"<< kms_temp<< ";"<<
consumo_temp<< ";"<< portagens_temp<< ";"<< num_veiculo_temp<< ";"<< preco_temp<< endl;

Frete f ( origem_temp, destino_temp, cliente_temp, kms_temp, consumo_temp, preco_temp, portagens_temp, num_veiculo_temp );
i.add_frete( f );

continuar = Interface::ler_int( "Deseja registar outro frete? ( 1:Sim / 0:Nao ) " );
system( "cls" );

}while ( continuar );

menu_inicial();
}
?

Well, the compiler is telling you that i is an undeclared identifier because nowhere in your code do you instantiate a variable named "i" yet you try to reference one by that name in the line

i.add_frete( f );

If this 'i' is declared in main you can't access it with other functions, but you can pass a pointer to it to the function that needs access to it. You could also declare it globally, but that is considered bad practice.
Topic archived. No new replies allowed.