【发布时间】:2014-06-14 18:18:04
【问题描述】:
我不知道如何在类中使用结构。我认为我的第一部分是正确的,但主要没有任何作用。当我尝试运行我的程序时,它说“函数不接受 0 个参数”我应该像这样在 main 中编写所有内容:
P.Read(BOX m);
到目前为止,这是我的代码:
#include <iostream>
#include <string>
using namespace std;
template <class T, int n>
class SIX
{
private:
struct BOX
{
T a[n];
string name;
};
public:
void Read(BOX m)
{
cout<<"Enter your name: ";
cin>>m.name;
cout<<m.name<<" please enter "<<n<<" data: ";
for(int i=0;i<n;++i)
cin>>m.a[i];
}
void SortArray(BOX m)
{
sort(m.a, m.a+n);
}
void Display(BOX m)
{
cout<<m.name<<" this is the sorted list of data in your array a: ";
for(int i=0;i<n;++i)
cout<<m.a[i]<<'\t';
cout<<endl;
}
};
int main()
{
SIX <int, 6> P;
SIX <string, 5> Q;
P.Read();
P.SortArray();
P.Display();
cout<<endl;
Q.Read();
Q.SortArray();
Q.Display();
cout<<endl;
system("pause");
return 0;
}
【问题讨论】: