簡體   English   中英

非成員函數'int find(const T&)'不能具有cv限定詞

[英]Non-member function 'int find(const T&)' cannot have cv-qualifier

我的函數原型如下所示:

// Purpose: finds an element in the ArrayList  

// Parameters: 'x' is value to be found in the ArrayList

// Returns: the position of the first occurrance of 'x' in the list, or -1  if 'x' is not found.

int find(const T& x) const;

它放置在ArrayList類中

template <typename T>
class ArrayList
{ 
  private:  
    int m_size;                          // current number of elements
    int m_max;                           // maximum capacity of array m_data
    T* m_data;                           // array to store the elements

    T m_errobj;                          // dummy object to return in case of error


  public:
    int find(const T& x) const;

我的定義是:

template <typename T>
int find(const T& x) const
{
  int i;
  while (m_data[i]!=x && m_data[i]!=NULL)
    i++;
  if (m_data[i]=x)
    return i;
  else
return (-1);
}

每次編譯時,都會在標題中收到錯誤,並且會在范圍中未聲明m_data的錯誤。 我該如何解決?

編輯:我將定義更改為

 int ArrayList<T>:: find(const T& x) const

我有很多錯誤

int ArrayList:: find(const T& x) const

也沒有用

模板必須在標題中定義。 就您而言,您將其拆分為.h / .cpp。 為了工作,您需要將其與類定義一起定義。 像這樣:

template <typename T>
class ArrayList
{ 
private:  
    int m_size;                          // current number of elements
    int m_max;                           // maximum capacity of array m_data
    T* m_data;                           // array to store the elements

    T m_errobj;                          // dummy object to return in case of error

public:
   int find(const T& x) const;
};

#include "x.hpp"

並在文件x.hpp中定義

template <typename T>
int ArrayList<T>::find(const T& x) const
{
   int i;
   while (m_data[i]!=x && m_data[i]!=NULL)
   i++;
   if (m_data[i]=x)
      return i;
   else
      return (-1);
}

請注意,這與在唯一的頭文件中定義所有內容的效果相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM