【发布时间】:2019-01-31 00:53:08
【问题描述】:
我无法将一个对象向量传递给另一个不同类型的对象向量。我要做的是创建一个 Partidas 对象,它显示 Producto 类型的对象的数组或向量,以及其他一些属性,但这些属性是整数和字符串,我对这些没有问题。当我运行我的程序时,当通过按“0”完成我的 las 循环时,它标志着 BAD ACCESS。我知道代码有点长,但我试图将所有内容都放在问题中,希望你们能帮助我。另外,如果您知道更好的方法来做我想做的事情,请告诉我。谢谢!
vector<Producto> vectorProductos;
while(file >> sID >> sDesc >> sUMed >> sFam >> sClass >> dVolumen >> dLongitud >> sPrecio){
vectorProductos.push_back(Producto(sID, sDesc, sUMed, sFam, sClass, dVolumen, dLongitud, stringToDouble(sPrecio)));
iNumProductos++;
}
file.close();
char cOption;
int iNumPartidas;
do{
cout << "Menu." << endl;
cout << "------------------" << endl;
cout << "a. Crear un proyecto." << endl;
cout << "b. Ver lista de proyectos." << endl;
cout << "c. Ver lista de productos existentes." << endl;
cout << "z. Terminar programa." << endl;
cin >> cOption;
switch(cOption){
case 'a':
cout << "Nombre de proyecto:\t";
cin >> sTitulo;
cout << "Numero de partidas de proyecto " << sTitulo << ":\t";
cin >> iNumPartidas;
cout << "Descripcion:\t";
cin >> sDescProyecto;
for(unsigned int iP = 1; iP <= iNumPartidas; iP++){
string sDescPart;
vector<Producto> vecProdxP;
vector<Partida> vectorPartidas;
cout << endl << "Partida " << iP << ":" << endl;
cout << "Clave de partida:\t";
cin >> sPartida;
cout << "Descripcion de Partida:\t";
cin >> sDescPart;
for(unsigned int iPrd = 0; iPrd < iNumProductos; iPrd++){
cout << endl << "Clave de producto " << iPrd+1 << ":\t";
cin >> sClave;
if(sClave != "0"){
for(int iC = 0; iC < vectorProductos.size(); iC++){
// int index = 0;
if(sClave == vectorProductos[iC].getClave()){
cout << "Cantidad:\t";
cin >> iCantProdxP;
file.open("/Users/javiersantisteban/Documents/Prueba/Prueba/ListaDePrecios.txt");
while(file >> sID >> sDesc >> sUMed >> sFam >> sClass >> dVolumen >> dLongitud >> sPrecio){
if(sID == sClave){
vecProdxP.push_back(Producto(sID, sDesc, sUMed, sFam, sClass, dVolumen, dLongitud, stringToDouble(sPrecio)));
}
}
file.close();
}
}
}else{
iPrd = iNumProductos;
}
}
vectorPartidas.push_back(Partida(sPartida, sDescPart, vecProdxP));
}
班级聚会
#ifndef Partidas_h
#define Partidas_h
#include <iostream>
#include <climits>
#include <string>
#include "Producto.h"
using namespace std;
class Partida{
private:
string _sPartida, _sDescripcion;//double _dPrecioPartida;
vector<Producto> _vecProd;
public:
Partida();
Partida(string, string, vector<Producto>);
inline string getClave(){return _sPartida;}
inline string getProy(){return _sDescripcion;}
inline void setClave(string sPartida){_sPartida = sPartida;}
inline void setProy(string sDescripcion){_sDescripcion = sDescripcion;}
void toString();
};
Partida::Partida(){
_sPartida = "";
_sDescripcion = "";
}
Partida::Partida(string sPartida, string sTitulo, vector<Producto> vecProd){
_sPartida = sPartida;
_sDescripcion = sTitulo;
for(unsigned int i = 0; i < vecProd.size(); i++){
_vecProd[i] = vecProd[i];
}
}
void Partida::toString(){
cout << "Clave de Partida:\t" << _sPartida << endl;
cout << "Proyecto:\t" << _sDescripcion << endl;
for(unsigned int i = 0; i < _vecProd.size();i++){
_vecProd[i].toString();
cout << endl;
}
}
#endif /* Partida_h */
【问题讨论】:
-
你有点忘了告诉我们问题出在哪里。
-
这个问题没有多大意义——向量不是函数,所以不要带参数。 “将参数传递给向量”是什么意思?
-
您需要创建一个minimal reproducible example。所有
couts 与问题有什么关系?菜单选择如何与问题相关?文件打开与问题有何关系?Partida中的所有方法如何与问题相关?哦,是的,正如轨道中的 Lightness Races 所说,您面临的实际问题是什么? -
@ChrisDodd Partidas 类具有一个 Producto 类型的向量作为属性,当用户想要查看“Partida”的“Productos(产品)”时,我需要显示一个 Productos 向量“ 拥有。对不起,如果我写错了。