我对C ++和类比较新。当我将char *传递给类构造函数并将其复制到m_make和m_model char *时,它们仍然是空的。当我打印出来时,它们并没有指向随机的内存位,没有数据输出,只是空白区域。 构造:
RentalCar::RentalCar(int year, char * make, char * model, float price, bool available)
{
m_year = year;
myStringCopy(m_make, make);
myStringCopy(m_model, model);
m_price = price;
m_available = available;
}
编辑:
void readFile(RentalAgencyADT * list, ifstream & file)
{
char letter;
char word[7];
int i = 0; //position in word
int j = 0; //position in RentalCar data
int k = 0; //position of line in RentalAgencyADT
int l = 0; //position of RentalAgencyADT
while((letter = file.get()) != EOF)
{
if(letter == ' ' || letter == '\n')
{
int year;
char make[7], model[7];
float price;
bool available;
i = 0;
if(k != 0)
{
switch(j)
{
case 0:
year = atoi(word);
j++;
break;
case 1:
myStringCopy(make,word);
j++;
break;
case 2:
myStringCopy(model,word);
j++;
break;
case 3:
price = atof(word);
j++;
break;
case 4:
available = (bool) atoi(word);
list[l].inventory[k - 1] = RentalCar(year,make,model,price,available);
j = 0;
k++;
break;
}
clearWord(word);
i = 0;
if(k == 7)
{
}
}
else if(k == 0)
{
switch(j)
{
case 0:
myStringCopy(list[l].name, word);
j++;
break;
case 1:
//intCopy((list[l].zipcode),word);
//cout << list[l].zipcode << endl;
for(int i = 0; i < 5; i++)
{
list[l].zipcode[i] = word[i] - '0';
cout << list[l].zipcode[i];
}
j = 0;
k++;
break;
}
clearWord(word);
}
if(j == 4)
{
clearWord(make);
clearWord(model);
}
}
else
{
word[i] = letter;
i++;
}
}
}
这是RentalCar.cpp文件:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "RentalCar.h"
using namespace std;
char* myStringCopy(char * destination, char * source);
RentalCar::RentalCar(int year, char * make, char * model, float price, bool available)
{
m_year = year;
myStringCopy(m_make, make);
myStringCopy(m_model, model);
m_price = price;
m_available = available;
}
char* myStringCopy(char * destination, char * source)
{
int i = 0;
while(source[i] != '\0')
{
destination[i] = source[i];
i++;
}
destination[i] = '\0';
return destination;
}
int RentalCar:: getYear()
{
return m_year;
}
void RentalCar:: setYear(int year)
{
m_year = year;
}
char* RentalCar:: getMake()
{
return m_make;
}
void RentalCar:: setMake(char * make)
{
myStringCopy(m_make, make);
}
char* RentalCar:: getModel()
{
return m_model;
}
void RentalCar:: setMode(char * model)
{
myStringCopy(m_model, model);
}
float RentalCar:: getPrice()
{
return m_price;
}
void RentalCar:: setPrice(int price)
{
m_price = price;
}
bool RentalCar:: getAvailable()
{
return m_available;
}
void RentalCar::setAvailable(bool available)
{
m_available = available;
}
void RentalCar::print()
{
cout << m_year << " " << m_make << " " << m_model << ", $" << m_price << ", Available: " << boolalpha << m_available << endl;
}
float RentalCar::estimateCost(int days)
{
return m_price * days;
}
我之前只是使用strcpy()函数进行测试,但它也没有用。
Headerfile:
#ifndef RENTALCAR_H_
#define RENTALCAR_H_
class RentalCar
{
public:
RentalCar(const int year, char * make, char * model, const float price, bool available);
int getYear();
void setYear(int year);
char* getMake();
void setMake(char make[]);
char* getModel();
void setMode(char model[]);
float getPrice();
void setPrice(int price);
bool getAvailable();
void setAvailable(bool available);
void print();
float estimateCost(int days);
protected:
private:
int m_year;
char * m_make;
char * m_model;
float m_price;
bool m_available;
};
#endif
答案 0 :(得分:1)
你是strcpy()
- 未初始化的char *
指针,这是未定义的行为。
至少你应该使用strdup()
,例如m_make = strdup(make);
,在析构函数中有相应的free()
。