简体   繁体   English

C++ 错误:'operator[]' 不匹配(操作数类型为 'const my_array_over' 和 'size_t' {aka 'long unsigned int'})

[英]C++ error: no match for 'operator[]' (operand types are 'const my_array_over' and 'size_t' {aka 'long unsigned int'})

#include <iostream>

using namespace std; 

class my_array_over
{

  size_t len = 1;

  int *a = new int[1];

public:
  my_array_over() { a[0] = 0; }

  my_array_over(size_t ln, const int *o) : len(ln), a(new int[ln])

  {
    for (size_t n = 0; n < ln; ++n)
      a[n] = o[n];
  }

  ~my_array_over() { delete[] a; }


  size_t get_length() const
  {
    return len;
  }

  int get(size_t n) const
  {
    return a[n];
  }

  int set(size_t n, int v)
  {
    int tmp = a[n];
    a[n] = v;
    return tmp;
  }

};

void foo(const my_array_over &a2, size_t i)
{

  if (i < a2.get_length())

    std::cout << a2[i] << std::endl;
}

Been trying to fix this code but kept getting an error saying "no match for 'operator[]' enter code here (operand types are 'const my_array_over' and 'size_t' {aka 'long unsigned int'})" on一直在尝试修复此代码,但一直收到错误消息,提示“不匹配 'operator[]' enter code here (操作数类型为 'const my_array_over' 和 'size_t' {aka 'long unsigned int'})”

std::cout << a2[i] << std::endl; std::cout << a2[i] << std::endl;

In this statement在这份声明中

std::cout << a2[i] << std::endl;

there is used the subscript operator for an object of the type my_array_over that (the subscript operator) is not defined within the class.对于my_array_over类型的 object 使用下标运算符(下标运算符)未在 class 中定义。 It seems you mean看来你的意思

std::cout << a2.get( i ) << std::endl;

Otherwise you need to define the subscript operator within the class definition.否则,您需要在 class 定义中定义下标运算符。 For example例如

const int & operator []( size_t n ) const
{
    return a[n];
}

int & operator []( size_t n )
{
    return a[n];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++字符串流错误:对于操作数类型std :: string和const unsigned int,运算符&lt;&lt;不匹配 - C++ stringstream error: no match for operator<< for operand types std::string and const unsigned int 如何理解C ++错误,“不匹配&#39;operator ==&#39;(操作数类型为&#39;std :: pair&#39;和&#39;const int&#39;)”? - How to understand C++ error, “no match for 'operator==' (operand types are 'std::pair' and 'const int')”? 错误:'operator&gt;&gt;' 不匹配(操作数类型是 'std::istream' {aka 'std::basic_istream<char> '} 和 'const int')|</char> - error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'const int')| C ++ [错误]不匹配'operator =='(操作数类型是'Vehicle'和'const Vehicle') - C++ [Error] no match for 'operator==' (operand types are 'Vehicle' and 'const Vehicle') xcode 构建失败 隐式转换丢失 integer 精度:“size_t”(又名“unsigned long”)到“socklen_t”(又名“unsigned int”) - xcode build failing over Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'socklen_t' (aka 'unsigned int') 如何解决隐式转换丢失整数精度:&#39;size_t&#39;(又名&#39;unsigned long&#39;)到&#39;int&#39;警告? - How to solve the Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' warning? iOS-隐式转换将整数精度&#39;size_t&#39;(aka&#39;unsigned long&#39;)转换为&#39;int&#39; - iOS - implicit conversion loses integer precision 'size_t' (aka 'unsigned long') to 'int' C++ 错误:“operator&lt;&lt;”不匹配(操作数类型为 - c++ error: no match for 'operator<<' (operand types are 如何 std::variant<unsigned long, size_t, unsigned int></unsigned> - how to std::variant<unsigned long, size_t, unsigned int> 错误:'operator+' 操作数类型不匹配是 'std::vector<int> c++ 中的 ' 和 'int'</int> - Error: no match for 'operator+' operand types are 'std::vector <int>' and 'int' in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM