【发布时间】:2018-01-16 16:48:27
【问题描述】:
我正在开发一个名为 Collection 的模板类,它只有和数组作为模板类的私有成员。我还需要实现其他类,但现在我正在研究其中一个类,称为 Hotel。
我应该创建一个像template class Type,int p_ 这样的模板,所以当我在主函数中调用Collection Hotel,5 h; 时,它应该创建一个酒店类型的数组,对吗?但是当我尝试这样做时,它只会创建一个酒店类型的对象并调用它的默认构造函数。
我可以使用一些建议来设置对象的值。酒店类有char* HotelName;int numRooms,openedYear,catagoryHotel。
template <class Type,int p>
class Collection
{
private:
Type* niz;
int n;//added this just in case
public:
Collection();
~Collection();
bool Find(Type t);//checks if the t is in the array with operator!=
void Arrange();//arranges the array
void Reverse();//swithces the first memmber of array to the last and the second to p-2
void Set(int i,Type t);//should set t to the niz[i]
void SaveElement(int i, char* Dat);//this is for saving in a file
void ReedElement(char*Dat);//this is for printing on the screan from a file
//bool operator!=(Type& t,Type& a);//visual studio is telling me 2 many arguments,dont know why
};
class Hotel
{
private:
char*HotelName;
int numRooms, openedYear,catagoryHotel;
public:
Hotel();
~Hotel();
void SetHotel();
//bool operator <=(Hotel& h1, Hotel& h2);this for some reason doesnt work also
};
//In my constructor for Collection i typed
template<class Type, int p>
inline Collection<Type, p>::Collection()
{
n = p;
niz = new Type[n];
}
//in the constructor for Hotel() i typed
Hotel::Hotel()
{
HotelName = "Undenfined";
catagoryHotel = 0;//how much stars it has
openedyear = 0;
numRooms = 0;
}
这是我为酒店输入的所有内容,在我弄清楚之前我不想再继续下去了。
【问题讨论】:
-
请注意缩进代码,使其尽可能可读。扁平缩进(即根本没有缩进)使得很难准确地知道一个块的开始位置和结束位置。
-
我想创建一个像“template class Type,int p”这样的模板,所以当我打电话给 Collection Hotel,5 h;在主函数中,它应该创建一个酒店类型的数组,对吗? 不,这是不正确的。请阅读a good book打下坚实的基础。
-
我还是这个网站的新手,对不起
-
我不清楚你到底在问什么,或者你遇到了什么问题。你有编译错误吗?你有运行时错误吗?你没有得到预期的结果吗?
-
无法编译 - 有几个错别字。 opensyear vs opensYear,“char* HotelName”可能应该是“std::string HotelName”可能应该使用酒店初始化列表。