班级的char*元素是空的



我对C 和类是相对较新的。当我将char*传递到类构造函数并将其复制到m_make和m_model char* s中时,它们仍然是空的。当我将内存打印出来时,他们没有指向随机的内存位,只输出数据,只是空白空间。 构造函数:

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] != '')
       {
           destination[i] = source[i];
           i++;
       }
       destination[i] = '';

       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;
   }

i较早使用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

您是 strcpy() -ing to未定义的char *指数,这是不确定的行为。

至少您应该使用strdup(),例如 m_make = strdup(make);,在破坏者中带有相应的 free()

最新更新