gpt4 book ai didi

c++ - 如何使用 MAP 创建类?? C++

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:35 25 4
gpt4 key购买 nike

救命!首先,对不起我的英语不好......但是,我的脑子被堵住了!我必须使用 map 创建一个类,我做到了但无法显示它,我可以插入但它不显示任何内容。

看看:

class Cliente{
private:
map <string, string> clientes;
map <string, string> autos;
map <string, string> tipcolor;
map <string, string> numeros;
friend class AutoServicio;

class AutoServicio{
private:
int i;
public:
AutoServicio();
void MostrarClientes();
void AgregarCliente();
};

AutoServicio::AutoServicio(){
i=0;
}

void AutoServicio::AgregarCliente(){
string nombre, telefono, marca, modelo, tipo, color, placa, kilometros;
map <string,string> clientes;
cout << "Nombre: ";
cin >> nombre;
cout << "Teléfono: ";
cin >> telefono;
clientes.insert(pair <string, string>(nombre, telefono));
clientes.insert(make_pair(nombre, telefono));

map <string, string> autos;
cout << "Marca: ";
cin >> marca;
cout << "Modelo: ";
cin >> modelo;
autos.insert(pair <string, string>(marca, modelo));
autos.insert(make_pair(marca, modelo));
map <string, string> tipcolor;
cout << "Tipo: ";
cin >> tipo;
cout << "Color: ";
cin >> color;
tipcolor.insert(pair <string, string>(tipo, color));
tipcolor.insert(make_pair(tipo, color));
map <string, string> numeros;
cout << "Placa: ";
cin >> placa;
cout << "Kilómetros: ";
cin >> kilometros;
numeros.insert(pair <string, string>(placa, kilometros));
numeros.insert(make_pair(placa, kilometros));
cout << "\n";
}

void AutoServicio::MostrarClientes(){ // **HERE IS MY PROBLEM! IT DOESN'T SHOW**
map <string,string> clientes;
map<string, string>::iterator p = clientes.begin();
cout << "Datos del cliente: ";
while (p != clientes.end() ){
cout << "\nNombre: " << p->first <<"\nTelefono: "<< p->second << endl;
p++;
}
map <string,string> autos;
map<string, string>::iterator p2 = autos.begin();
cout << "\nDatos del auto: ";
while (p2 != autos.end() ){
cout << "\nMarca: " << p2->first <<"\nModelo: "<< p2->second << endl;
p2++;
}
map <string,string> tipcolor;
map<string, string>::iterator p3 = tipcolor.begin();
while (p3 != tipcolor.end() ){
cout << "\nTipo: " << p3->first <<"\nColor: "<< p3->second << endl;
p3++;
}

map <string,string> numeros;
map<string, string>::iterator p4 = numeros.begin();
while (p4 != numeros.end() ){
cout << "\nPlaca: " << p4->first <<"\nKilómetros: "<< p4->second << endl;
p4++;
}
}

谢谢!!!

最佳答案

AutoServicio::AgregarCliente() 中,删除以下内容:

map <string,string> clientes;

并删除这个

map <string, string> autos;

并删除这个

map <string, string> tipcolor;

并删除这个

map <string, string> numeros;

C++ 允许您拥有同名的不同变量。因为您有一个名为 numeros(等)的局部变量,所以当您在该函数内说 numeros 时,您是在谈论局部变量,而不是成员变量,即使您想要成员变量。

关于c++ - 如何使用 MAP 创建类?? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33794462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com