繁体   English   中英

错误:赋值中的非左值

[英]Error : non-lvalue in assignment

#include <iostream>
using namespace std;

class Array
{
   friend ostream &operator<<( ostream &, const Array & );
public:
   Array( int = 5 ); 
   Array( const Array & ); 
   ~Array(); 
   int getSize() const; 
   const Array &operator=( const Array & );
   // subscript operator for non-const objects returns modifiable lvalue
   int &operator[]( int );              
   // subscript operator for const objects returns rvalue
   int operator[]( int ) const;  
private:
   int size; 
   int *ptr; 
}; 

Array::Array( int arraySize )
{
   size = ( arraySize > 0 ? arraySize : 5 ); // validate arraySize
   ptr = new int[ size ]; 

   for ( int i = 0; i < size; i++ )
      ptr[ i ] = 0; 
}

// must receive a reference to prevent infinite recursion
Array::Array( const Array &arrayToCopy ) 
   : size( arrayToCopy.size )
{
   ptr = new int[ size ]; // create space for pointer-based array

   for ( int i = 0; i < size; i++ )
      ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object
} 

Array::~Array()
{
   delete [] ptr; // release pointer-based array space
} 

int Array::getSize() const
{
   return size; // number of elements in Array
} 

const Array &Array::operator=( const Array &right )
{
   if ( &right != this ) // avoid self-assignment
   {
      if ( size != right.size )
      {
         delete [] ptr; // release space
         size = right.size; // resize this object
         ptr = new int[ size ]; // create space for array copy
      }

      for ( int i = 0; i < size; i++ )
         ptr[ i ] = right.ptr[ i ]; // copy array into object
   }

   return *this; 
}

// overloaded subscript operator for non-const Arrays reference return creates a modifiable lvalue
int &Array::operator[]( int subscript )
{
cout << " ***************Inside non-sonstant operator[] function: Lvalue test*********** ";
   if ( subscript < 0 || subscript >= size )
   {
      cerr << "\nError: Subscript " << subscript 
         << " out of range" << endl;
      exit( 1 ); // terminate program; subscript out of range
   }

   return ptr[ subscript ]; // reference return
}

// overloaded subscript operator for const Arrays const reference return creates an rvalue
int Array::operator[]( int subscript ) const
{
cout << " ***************Inside sonstant operator[] function: Rvalue test*********** ";
   if ( subscript < 0 || subscript >= size )
   {
      cerr << "\nError: Subscript " << subscript 
         << " out of range" << endl;
      exit( 1 ); 
   } 

   return ptr[ subscript ]; // returns copy of this element
}


// overloaded output operator for class Array 
ostream &operator<<( ostream &output, const Array &a )
{
   int i;
   // output private ptr-based array 
   for ( i = 0; i < a.size; i++ )
   {
      output << a.ptr[ i ] << " ";

      if ( ( i + 1 ) % 4 == 0 )
         output << endl;
   } // end for

   if ( i % 4 != 0 ) 
      output << endl;

   return output; 
} 

int main()
{
   Array integers1( 4 ); 
   Array integers2; // 5-element Array by default
   const Array& integers4=integers1;
    //integers4[3] = 2000; //Error : non-lvalue in assignment 
    integers1 = integers1;  //valid
    integers4 = integers1;  //Error : binary '=' : no operator found 
    //which takes a left-hand operand of type 'const Array' (or there is no 
    //acceptable conversion)
    cout << "\nintegers1[3] is " << integers4[ 3 ];

   return 0;
} 

给出错误:

1)在函数'int main()'中:

'=':左操作数必须为左值

2)二进制'=':找不到使用'const Array'类型的左操作数的运算符(或者没有可接受的转换)

请帮忙。

您不能修改const引用,请尝试以下操作:

//Snippet1
Array& integers4=integers1;
integers4[3] = 2000; 

为了澄清OP关于修改const引用的疑问:

//Snippet2
//This will not compile as you saw.
const Array& integers4=integers1;
integers4[3] = 2000; //errors

现在,这就是Xeo在回复此帖子时所做 他没有更改引用,而是更改了原始变量。

//Snippet2
//This will compile and work identically to Snippet1.
const Array& integers4=integers1;
interger1[3] = 2000; 

第一个错误integers4是const Array因此您只能使用const operator[] ,它返回int而不是int& 您不能将其分配给返回的临时int ,也不能更改为const的内容。
第二个错误可能是第一个错误的衍生。

在特定的代码段上:

Array integers1( 4 ); 
const Array& integers4=integers1;
//integers4[3] = 2000; //Error : non-lvalue in assignment 

您正在使用对数组的常量引用来调用成员函数,并且编译器将对int Array::operator[]( int x ) const的调用解析(另一版本的operator[]需要一个非const对象) 。 该表达式产生一个右值,您不能将其赋值。

integers4 = integers1;  //Error : binary '=' : no operator found 

再有一个问题是, integers4是一个常量的左值,这意味着它不能被修改(因为其中存在const )。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM