您好我有两个定义类的文件。 在其中一个我尝试使用指针声明另一个类的数组的数组,我有一些语法的麻烦,我如何在第一个文件中声明这个?提前谢谢你
第一个文件:
#ifndef YARD_H_INCLUDED
#define YARD_H_INCLUDED
#include <iostream>
using namespace std;
class yard
{
public:
int yard_size;
car *car_array[];
int cars_in_yard;
yard();
void show() const
{
for (int i=0; i<=cars_in_yard;i++)
{
car_array[i]->show();
}
}
~yard()
{
yard_size=0;
delete car_array[cars_in_yard];
cars_in_yard= 0;
}
};
#endif // YARD_H_INCLUDED'
第二个是:
#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class car
{
public:
char car_name[80];
double price;
int eng;
car();
void show() const
{
cout<<"Car Make : "<< *car_name <<" , Price: "<< price<<" , Engine : "<< eng <<" , cc";
}
};
car::car()
{
car_name[0]=0;
price=0.0;
eng=0;
}
#endif // CAR_H_INCLUDED'
答案 0 :(得分:1)
car *car_array[];
是语法错误。必须在编译时知道数组大小。要使用运行时大小调整,您需要使用动态内存分配。
基于Yard
的析构函数;似乎这个阵列中的汽车应该“属于”场地 - 即场地负责清理它们。
让Yard存储一个car
数组会更简单,而不是存储car *
数组。但我假设您决定存储一组car *
而不是。
当然,最简单的方法是std::vector<car*> car_array;
(或者可以说是unique_ptr
的向量)。但是,由于你应该重新发明轮子,你需要做以下事情:
car **car_array;
nullptr
)yard_size
和cars_in_yard
设为私有 - 您的Yard需要能够准确地跟踪正在使用的内存量yard_size
更新时,如果它变得更大,您需要new[]
一些新空间car *
'复制来自旧空间的值,并delete[]
旧空间cars_in_yard
增加时,您需要检查是否需要增加yard_size
cars_in_yard
减少时,您需要删除不再在院子里的汽车,并将所有内容拖放,以便没有间隙(除非您当然想要允许间隙,但是您的插入逻辑是更复杂)car_array
。答案 1 :(得分:0)
尝试这种邪恶的技巧:
#define MAX_NAME_LENGTH 64
struct car
{
char * p_name;
car() : p_name(NULL)
{ }
car(char const * const p_new_name)
{
if (p_name)
{
delete [] p_name;
}
const unsigned int size = strlen(p_new_name) + 1; // Remember that terminating nul char
p_name = new char(size);
strcpy(p_name, p_new_name); // Make a copy of the parameter string
}
};
如果您真的想要正确使用动态内存,那么您的课程将教您使用智能或唯一指针。
该技术是邪恶的,因为它没有使用使用动态内存的测试数据结构,例如std::string
或std::vector
。
编辑1:
是的,邪恶,因为析构函数不存在,编译器生成的析构函数不会释放动态内存。邪恶,因为它可能导致上述代码中的错误。
添加构造函数:
~car()
{
delete [] p_name; // Note position of []
}
答案 2 :(得分:0)
#ifndef YARD_H_INCLUDED
#define YARD_H_INCLUDED
#include "car.h"
#include <iostream>
using namespace std;
class yard
{
private:
int yard_size;
car **car_array;
int cars_in_yard;
public:
yard(int t_yard_size)
{
yard_size=t_yard_size;
car_array= new car *[yard_size];
cars_in_yard=0;
}
~yard()
{
for(int i=0; i<cars_in_yard; i++)
{
delete car_array[i];
}
}
void show() const
{
for (int i=0; i<cars_in_yard; i++)
{
car_array[i]->show();
}
}
int num_Of_cars()
{
return cars_in_yard;
}
void add_car(car *);
void show_last();
bool place();
};
#endif // YARD_H_INCLUDED
yard.h的Cpp
#include "yard.h"
#include "car.h"
void yard::add_car(car *t_car)
{
if (yard_size == cars_in_yard)
{
cout<< "There is no place in a Yard"<<endl;
}
else
{
car_array[cars_in_yard++] = t_car;
cout<<"New car added successfully"<<endl;
}
}
void yard::show_last()
{
car_array[cars_in_yard-1]->show();
}
bool yard::place()
{
if (yard_size==cars_in_yard)
{
cout<<"There is no place in a Yard"<<endl;
return false;
}
else
{
return true;
}
}
最后是car.h
#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED
#include <iostream>
using namespace std;
class car
{ private:
char car_name[80];
double price;
int eng;
public:
car()
{
price=0.0;
eng=0;
}
car(char t_car_name[],int t_price, int t_eng)
{
strcat(car_name, t_car_name);
price = t_price;
eng = t_eng;
}
void show()
{
std::cout<<"Car Make : "<< car_name <<" , Price: "<< price<<" $ , Engine : "<< eng <<" cc ."<<std::endl;
}
};
#endif // CAR_H_INCLUDED