如何 return C++ 中的模板列表?
how to return a template list in C++?
我在学校和家庭作业中学习 C++,我的任务是为这些创建 FooClass:
int main()
{
int x[] = {3, 7, 4, 1, 2, 5, 6, 9};
FooClass<int> ui(x, sizeof(x) / sizeof(x[0]));
std::string s[] = {"Car", "Bike", "Bus"};
FooClass<std::string> us(s, sizeof(s) / sizeof(s[0]));
}
然后修改代码,使其可以写出列表的大小和列表的元素。
我设法编写了大小函数的代码。
但是我在元素部分苦苦挣扎,得到一个
error: missing template arguments before '.' token.
到目前为止,这是我的代码:
template <typename T>
class FooClass
{
private:
T *items;
int itemsSize;
bool mergeOn;
public:
FooClass(T items[], int itemsSize)
{
items = new T[itemsSize];
this->itemsSize = itemsSize;
};
int getItemsSize()
{
return this->itemsSize;
}
void print(const FooClass <T>& items)
{
for (int i=0; i< items.getItemsSize(); ++i)
{
std::cout<<items[i]<<std::endl;
}
}
};
int main()
{
int x[] = {3, 7, 4, 1, 2, 5, 6, 9};
FooClass<int> ui(x, sizeof(x) / sizeof(x[0]));
std::string s[] = {"Car", "Bike", "Bus"};
FooClass<std::string> us(s, sizeof(s) / sizeof(s[0]));
std::cout<<ui.getItemsSize()<<std::endl;
FooClass.print(us); //this is where I get the compilation error.
}
如何实现打印功能?
您的构造函数没有将源元素复制到它分配的数组中。而且,当你使用完分配的数组时,你需要一个析构函数来释放它。
而且,您的 print()
方法不是 static
,因此它应该作用于 this
而不是将 FooClass
对象作为参数。
试试这个:
template <typename T>
class FooClass
{
private:
T *m_items;
int m_itemsSize;
bool m_mergeOn;
public:
FooClass(T items[], int itemsSize)
{
m_items = new T[itemsSize];
for (int i = 0; i < itemsSize; ++i) {
m_items[i] = items[i];
}
m_itemsSize = itemsSize;
};
~FooClass()
{
delete[] m_items;
}
int getItemsSize() const
{
return m_itemsSize;
}
void print() const
{
for (int i = 0; i < m_ItemsSize; ++i)
{
std::cout << m_items[i] << std::endl;
}
}
};
int main()
{
int x[] = {3, 7, 4, 1, 2, 5, 6, 9};
FooClass<int> ui(x, sizeof(x) / sizeof(x[0]));
std::cout << ui.getItemsSize() << std::endl;
ui.print();
std::string s[] = {"Car", "Bike", "Bus"};
FooClass<std::string> us(s, sizeof(s) / sizeof(s[0]));
std::cout << us.getItemsSize() << std::endl;
us.print();
}
我在学校和家庭作业中学习 C++,我的任务是为这些创建 FooClass:
int main()
{
int x[] = {3, 7, 4, 1, 2, 5, 6, 9};
FooClass<int> ui(x, sizeof(x) / sizeof(x[0]));
std::string s[] = {"Car", "Bike", "Bus"};
FooClass<std::string> us(s, sizeof(s) / sizeof(s[0]));
}
然后修改代码,使其可以写出列表的大小和列表的元素。
我设法编写了大小函数的代码。 但是我在元素部分苦苦挣扎,得到一个
error: missing template arguments before '.' token.
到目前为止,这是我的代码:
template <typename T>
class FooClass
{
private:
T *items;
int itemsSize;
bool mergeOn;
public:
FooClass(T items[], int itemsSize)
{
items = new T[itemsSize];
this->itemsSize = itemsSize;
};
int getItemsSize()
{
return this->itemsSize;
}
void print(const FooClass <T>& items)
{
for (int i=0; i< items.getItemsSize(); ++i)
{
std::cout<<items[i]<<std::endl;
}
}
};
int main()
{
int x[] = {3, 7, 4, 1, 2, 5, 6, 9};
FooClass<int> ui(x, sizeof(x) / sizeof(x[0]));
std::string s[] = {"Car", "Bike", "Bus"};
FooClass<std::string> us(s, sizeof(s) / sizeof(s[0]));
std::cout<<ui.getItemsSize()<<std::endl;
FooClass.print(us); //this is where I get the compilation error.
}
如何实现打印功能?
您的构造函数没有将源元素复制到它分配的数组中。而且,当你使用完分配的数组时,你需要一个析构函数来释放它。
而且,您的 print()
方法不是 static
,因此它应该作用于 this
而不是将 FooClass
对象作为参数。
试试这个:
template <typename T>
class FooClass
{
private:
T *m_items;
int m_itemsSize;
bool m_mergeOn;
public:
FooClass(T items[], int itemsSize)
{
m_items = new T[itemsSize];
for (int i = 0; i < itemsSize; ++i) {
m_items[i] = items[i];
}
m_itemsSize = itemsSize;
};
~FooClass()
{
delete[] m_items;
}
int getItemsSize() const
{
return m_itemsSize;
}
void print() const
{
for (int i = 0; i < m_ItemsSize; ++i)
{
std::cout << m_items[i] << std::endl;
}
}
};
int main()
{
int x[] = {3, 7, 4, 1, 2, 5, 6, 9};
FooClass<int> ui(x, sizeof(x) / sizeof(x[0]));
std::cout << ui.getItemsSize() << std::endl;
ui.print();
std::string s[] = {"Car", "Bike", "Bus"};
FooClass<std::string> us(s, sizeof(s) / sizeof(s[0]));
std::cout << us.getItemsSize() << std::endl;
us.print();
}