我对模板和重载的使用不是很熟悉,我被要求纠正一个代码作为练习。我纠正了所有我能识别的内容,但我不确定我忽略了什么。如果变量名称对您来说听起来很糟糕,我深表歉意,它们都带有原始损坏的代码。
部分错误信息是
CSL.cpp:在成员函数“void CSL::showList() [with T = int]”中:
CSL.cpp:106:从这里实例化 CSL.cpp:26:错误:下标值既不是数组也不是指针
代码本身:
template<class T>
CSL<T>::CSL(T *d, int s) : data(*d), size(s)
{
}
template<class T>
void CSL<T>::showList()                 //Function with problem.
{
  cout<<"Comma separated list:"<<endl;
  for(int x = 0; x < size; ++x)
  {
    cout << data[x];
    if(x != size + 1)
      cout << ": ";
  }
  cout << endl << endl;
}
int main()
{
  someCustomers[0].setCustomer("Zaps", 23.55);
  //...
  someCustomers[5].setCustomer("Curtin",56999.19);
  CSL_Size = sizeof(someInts)/sizeof(someInts[0]);
  CSL<int> CSL_Integers(someInts, CSL_Size);
  //...    
  CSL_Size = sizeof(someCustomers)/sizeof(someCustomers[0]);
  CSL<Customer> CSL_Customers(someCustomers, CSL_Size);
  CSL_Integers.showList();   //Problem starts here
  CSL_Doubles.showList();
  CSL_Videos.showList();
  CSL_Customers.showList();
  return 0;
}