【问题标题】:Error: expected constructor, destructor, or type conversion before '&' token错误:“&”标记之前的预期构造函数、析构函数或类型转换
【发布时间】:2014-05-04 02:53:23
【问题描述】:

标题确实说明了一切,我遇到了这个错误,我只是不知道在 & 之前放什么才能让它发挥作用?

我只使用了我认为必要的部分头文件和cpp文件

标题:

#include <iostream>
using namespace std;


#ifndef INDEXLIST_H
#define INDEXLIST_H

template <class T>
class indexList
{
 public:
  //constuctor, default
  //Descriptions: Initializes numberOfElement to 0
  //              Initializes maxSize to 100
  //Parameters:   none
  //Return:       none

  indexList(int size = 10);

  //copy constructor
  indexList(const indexList &rhs);

  //destructor
  ~indexList();

  //assignment operator
  indexList &operator=(const indexList &rhs);

cpp:

//assignment operator
template <class T>
 // error occurs on the line below
indexList &indexList::operator=(const indexList &rhs)
{
  if(*this != rhs)
    {
      delete [] list;
      numberOfElement = rhs.numberOfElement;
      maxSize = rhs.maxSize;
      list = new T[maxSize];
      for(int i = 0; i < numberOfElements; i++)
        {
        list[i] = rhs.list[i];
        }

      return *this;

    }

【问题讨论】:

  • 哪一行显示错误?
  • cpp第三行

标签: c++ templates assignment-operator


【解决方案1】:

语法中缺少一对&lt;T&gt;

//assignment operator
template <class T>
indexList<T> &indexList<T>::operator=(const indexList &rhs)
{
    // ...
}

另外,这个定义和所有模板函数一样,应该在头文件中定义。

【讨论】:

  • 是不是在//赋值运算符下没有定义
  • 是的,但请注意您需要在哪里使用indexList&lt;T&gt;,而不仅仅是indexList
猜你喜欢
  • 2018-12-04
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多