我的程序运行正常,标准内存分配,但使用动态内存分配使程序行为奇怪!它甚至不会从主要功能打印任何东西。
在头文件中解析了指向alist
的指针,并在类defination(构造函数)中使用new
动态分配了内存。用魔女程序指出代码工作正常。还使用了许多couts进行测试,忽略它们。感谢
标题文件:
//Vehicle.h
//Author: Ankush Kaul
//Purpose: Declaration of Vehicle class
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
#include <fstream>
using namespace std;
class Vehicle
{
public:
Vehicle(); //default constructor
Vehicle(ifstream&); //parametrized constructor
Vehicle(const Vehicle& other); //copy constructor
~Vehicle(); // destructor
//over loaded operators
Vehicle& operator=(const Vehicle& other); // assignment operator
bool operator==(const Vehicle& other); // relational operator
// accessors
int getVid();
string getManufact();
string getColor();
double getCost();
int getnumAccessories();
string getList();
// 3 functions to access accessory list
void startAcc();
string nextAcc();
bool hasNextAcc();
protected:
private:
int vId;
string manufact;
string color;
double cost;
int numAccessories;
string *alist; // acessory list
//string alist[10]; <- WORKS WITH THIS
int counter; // hidden object used for accessing accesory list
};
#endif // VEHICLE_H
班级定义:
//Vehicle.h
//Author: Ankush Kaul
//Purpose: Implementation of Vehicle class
#include "Vehicle.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
using std::atoi;
using std::atof;
using std::ifstream;
using std::cout;
//---------------------------
//Name: Vehicle
//Purpose: default constructor
//Parameters: None
//Return: None
//---------------------------
Vehicle::Vehicle()
{
vId = 999;
manufact = color = "";
cost = numAccessories = 0;
}
//---------------------------
//Name: Vehicle()
//Purpose: Parametrized constructor
//Parameters:
// fin : input file stream
//Return: None
//---------------------------
Vehicle::Vehicle(ifstream &fin)
{
string line;
getline(fin, line);
vId = atoi(line.c_str());
getline(fin, line);
manufact= line;
getline(fin, line);
color = line;
getline(fin, line);
cost = atof(line.c_str());
getline(fin, line);
numAccessories = atoi(line.c_str());
alist = new string[numAccessories]; <-- WORKS WITHOUT THIS
cout<<"**";
for (int i = 0; i < numAccessories; i++)
{
getline(fin, line);
alist[i] = line;
}
}
//---------------------------
//Name: ~Vehicle
//Purpose: Destructor
//Parameters: None
//Return: None
//---------------------------
Vehicle::~Vehicle()
{
}
//---------------------------
//Name: Vehicle()
//Purpose: Copy constructor
//Parameters:
// other : Vehicle object passed to copy
//Return: None
//---------------------------
Vehicle::Vehicle(const Vehicle& other)
{
this->vId = other.vId;
this->manufact = other.manufact;
this->color = other.color;
this->cost = other.cost;
this->numAccessories = other.numAccessories;
for ( int i = 0 ; i < other.numAccessories ; i++ )
this->alist[i] = other.alist[i];
}
//---------------------------
//Name: operator=
//Purpose: Overloading operator
//Parameters:
// other : Vehicle object passed to assign values
//Return: Vehicle
//---------------------------
Vehicle& Vehicle::operator=(const Vehicle& other)
{
this->vId = other.vId;
this->manufact = other.manufact;
this->color = other.color;
this->cost = other.cost;
this-> numAccessories = other.numAccessories;
for ( int i = 0 ; i < other.numAccessories ; i++ )
this->alist[i] = other.alist[i];
return *this;
}
//---------------------------
//Name: operator==
//Purpose: Overloading operator
//Parameters:
// other : Vehicle object passed to compare
//Return: bool
//---------------------------
bool Vehicle::operator==(const Vehicle& other)
{
if (this->vId == other.vId &&
this->manufact == other.manufact &&
this->color == other.color &&
this->cost == other.cost &&
this->numAccessories == other.numAccessories)
{
return true;
}
return false;
}
//---------------------------
//Name: getVid
//Purpose: gets vId attribute
//Parameters: none
//Return: int
//---------------------------
int Vehicle::getVid()
{
return vId;
}
//---------------------------
//Name: getManufact
//Purpose: gets manufact attribute
//Parameters: none
//Return: string
//---------------------------
string Vehicle::getManufact()
{
return manufact;
}
//---------------------------
//Name: getColor
//Purpose: gets color attribute
//Parameters: none
//Return: string
//---------------------------
string Vehicle::getColor()
{
return color;
}
//---------------------------
//Name: getCost
//Purpose: gets cost attribute
//Parameters: none
//Return: double
//---------------------------
double Vehicle::getCost()
{
return cost;
}
//---------------------------
//Name: getnumAccessories
//Purpose: gets numAccessories attribute
//Parameters: none
//Return: int
//---------------------------
int Vehicle::getnumAccessories()
{
return numAccessories;
}
// 3 functions to get accesory list
//---------------------------
//Name: startAcc
//Purpose: sets counter to zero
//Parameters: none
//Return: void
//---------------------------
void Vehicle::startAcc()
{
counter = 0;
}
//---------------------------
//Name: nextAcc
//Purpose: returns name of accesory
//Parameters: none
//Return: string
//---------------------------
string Vehicle::nextAcc()
{
return alist[counter++];
}
//---------------------------
//Name: hasNextAcc
//Purpose: checks if all accessory list is read
//Parameters: none
//Return: bool
//--------------------------
bool Vehicle::hasNextAcc()
{
if ( counter < numAccessories)
return true;
return false;
}
主档案:
//main.cpp
//Topic: Project 4 - Vehicle
//Author: Ankush Kaul
//Purpose: Read and display list of Vehicles
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include "Vehicle.h"
using namespace std;
void printVehicle (Vehicle);
int main ()
{
ifstream fin; // holds input file stream
string file,line; // file - holds filename, line - holds line read from file
cout<<"Enter filename : ";
cout<<"()";
//cin>>file;
cout<<"11";
//fin.open(file.c_str());
fin.open("vehicle1.txt");
cout<<"^^";
// checking if file is sucessfully opened
if (!fin.is_open())
{
cout<<"Error opening file!";
return 1;
}
cout<<"@@";
//cin.ignore(1000, '\n');
cout<<"%%";
getline(fin, line); // reading the first line which contains number of vehicles in the file
cout<<"!!";
int count = atoi(line.c_str());
cout<<"&&";
Vehicle veh[count]; // defining a array of length = count
// loop to read details of vehicles from file and store it in the veh[] array
for (int i = 0; i < count; i++)
{
cout<<"##";
Vehicle newVeh(fin);
bool duplicate = false; // sets duplicate flag to false
// loop to check if a duplicate vehicle exists
for (int k = 0; k < i; k++)
if (newVeh == veh[k])
{
duplicate = true;
cout<<endl<<"Duplicate Vehicle detected --- ignored!"<<endl;
}
if (duplicate)
continue;
veh[i] = newVeh;
}
// print list of vehicles to the screen by calling printVehicle function
cout<<endl<<"Vehicle List";
for (int i = 0; i < count; i++)
{
//cout<<endl<<"in loop "<<i<<endl;
cout<<endl<<"-------------------------";
printVehicle(veh[i]);
}
return 0;
}
//---------------------------
//Name: printVehicle
//Purpose: prints the vehicle details
//Parameters: veh
//Return: void
//---------------------------
void printVehicle(Vehicle veh)
{
cout<<endl<<"Vin: "<<veh.getVid();
cout<<endl<<"Manufacturer: "<<veh.getManufact();
cout<<endl<<"Color: "<<veh.getColor();
cout<<fixed<<showpoint<<setprecision(2); // output modifier to print decimal with 2 point accuracy
cout<<endl<<"Cost: "<<veh.getCost();
cout<<endl<<"Accessory List:"<<endl;
// printing accessory list
veh.startAcc();
while(veh.hasNextAcc())
{
string nextAcc = veh.nextAcc();
cout<<" "<<nextAcc<<endl;
}
}
答案 0 :(得分:2)
您的复制构造函数和赋值运算符已损坏。您没有为附件列表分配内存。您的复制构造函数应如下所示:
Vehicle::Vehicle(const Vehicle& other)
:
vId (other.vId),
manufact (other.manufact),
color (other.color),
cost (other.cost),
numAccessories (other.numAccessories),
alist (0),
counter (0)
{
alist = new string[numAccessories];
for ( int i = 0 ; i < other.numAccessories ; ++i ) {
alist[i] = other.alist[i];
}
}
您的赋值运算符应该在赋值之前类似地分配alist
。您的赋值运算符还有另一个问题,即您没有解决,这是自我分配问题。如果有人vehicle = vehicle;
怎么办?
您的析构函数也不正确。您应该释放由构造函数或赋值运算符分配的内存。
您的比较运算符没有被破坏,但它也可能不正确。据推测,您应该测试两辆车是否具有相同的配件,而不仅仅是测试它们具有相同数量的配件。