lvalue required as left operand of assignment

I’m getting errors when trying to add two data type SmartArrays together. I think the error is within the assignment operator but I can’t seem to figure out the problem. Below are two functions from the templated class SmartArray. When attempting to run unit tests, I get the errors at the bottom of the page.
Is there something I’m missing? Thank you in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SmartArray& operator= (const SmartArray& right){
     if(this == right){
       return this;
     }
     delete[] this->things;

     things = new T[SMART_ARRAY_CAPACITY];
     for (unsigned int i = 0; i < right.size(); i++){
       things[i] = right[i];
     }
}

SmartArray operator+ (const SmartArray& right){
     SmartArray ret;
     for (unsigned int i = 0; i++; i < right.size()){
       ret[i] = right[i];
     }
     if ((right.size() + s) < SMART_ARRAY_CAPACITY){
       for (unsigned int i = 0; i < right.size(); i++){
         ret[s+i] = right[i];
       }
     }
     else{
       throw logic_error("Not enough room in array");
     }
     return ret;
}



I also tried it as ret = right without the loop in the + function, but that didn’t work either.

The errors I receive are below:

In file included from SmartArray_hw_test.h:4:0,
from testrunner.cpp:23:
SmartArray.h: In instantiation of ‘SmartArray<T> SmartArray<T>::operator+(const SmartArray<T>&) [with T = int]’:
SmartArray_hw_test.h:89:5: required from here
SmartArray.h:122:16: error: lvalue required as left operand of assignment
ret[i] = right[i];
^
SmartArray.h:126:20: error: lvalue required as left operand of assignment
ret[s+i] = right[i];
^
make: *** [testrunner] Error 1

Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

Your operator[] function is implemented incorrectly - it should return a reference.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef SMARTARRAY_H
#define SMARTARRAY_H
/*
	A smart array that can resize up to 1000 elements.
	Krish Shah
	September 2015
*/

#include <string>
#include <ostream>
#include <stdexcept>

using namespace std;

template <class T>
class SmartArray{

  public:
    SmartArray(){
      s = 0;
      SMART_ARRAY_CAPACITY = 1000;
      things = new T[SMART_ARRAY_CAPACITY];
    };
    
    SmartArray(unsigned int i, const T& thing){
      s = 0;
      SMART_ARRAY_CAPACITY = 1000;
      things = new T[SMART_ARRAY_CAPACITY];
      if (i < SMART_ARRAY_CAPACITY){
        for (unsigned int x = 0; x < i; x++){
          things[x] = thing;
          s++;
        }
      }
      else{
        throw logic_error("Maximum array size: 1000");
      }
    };
    
    SmartArray(const SmartArray& right){
      s = right.size();
      things = new T[1000];
      for (unsigned int i = 0; i < s; i++){
        things[i] = right[i];
      }
    }
    
    SmartArray& operator= (const SmartArray& right){
      if(this == right){
        return this;
      }
      delete[] this->things;
      
      things = new T[SMART_ARRAY_CAPACITY];
      for (unsigned int i = 0; i < right.size(); i++){
        things[i] = right[i];
      }
    }
    
    
    ~SmartArray(){
      delete[] things;
    }
    
    void push_back(T thing){
      if (s < SMART_ARRAY_CAPACITY){
        things[s] = thing;
        s++;
      }
      else{
        throw logic_error("No more room in array");
      }
    }
    
    unsigned int size() const{
      return s;
    }
    
    T back(){
    if (s == 0){
      throw logic_error("Array is empty");
    }
      return things[s-1];
    }
    
    void pop_back(){
      if (s == 0){
        throw logic_error("Array is already empty");
      }
      else{
        s--;
      }
    }
    
    T at(int i)const {
      if (i < 0){
        i = s+i;
      }
      if (i < s){
        return things[i];
      }
      else{
        throw logic_error("Index not within array limits"); 
      }
    }
    
    void clear(){
      s = 0;
    }
    
    SmartArray reverse(){
      SmartArray ret;
      for (unsigned int i = 0; i < s; i++){
        ret[i] = things[s - i];
      }
      return ret;
    }
    
    SmartArray operator+ (const SmartArray& right){
      SmartArray ret;
      for (unsigned int i = 0; i++; i < right.size()){
        ret[i] = right[i];
      }
      if ((right.size() + s) < SMART_ARRAY_CAPACITY){
        for (unsigned int i = 0; i < right.size(); i++){
          ret[s+i] = right[i];
        }
      }
      else{
        throw logic_error("Not enough room in array");
      }
      return ret;
    }
    
    bool operator== (const SmartArray& right){
      if (s == right.size()){
      unsigned int x = 0;
        for (unsigned int i = 0; i < s; i++){
          if (things[i] == right[i]){
            x++;
          }
        }
        if (x == s){
          return true;
        }
        else{
          return false;
        }
      }
    }
    
    bool operator!= (const SmartArray& right){
      if (s == right.size()){
      unsigned int x = 0;
        for (unsigned int i = 0; i < s; i++){
          if (things[i] == right[i]){
            x++;
          }
        }
        if (x == s){
          return false;
        }
        else{
          return true;
        }
      }
      else{
        return true;
      }
    }
    
    T operator[](int i)const {
      return at(i);
    } 
  
    
    private:
    
    T* things;
    unsigned int s;
    unsigned int capacity;
    unsigned int SMART_ARRAY_CAPACITY;

  
};

template <class T>
ostream& operator<< (ostream& out, const SmartArray<T>& thing){
  if (thing.size() != 0){
    out << "[";
    for (unsigned int i = 0; i < thing.size() - 1; i++){
      out << thing[i] << ", ";
    }
    out << thing[thing.size()-1] << "]";
  }
  else{
    out << "[]\n";
  }
  return out;
}

#endif 


This is the full class -
My operator[] function calls another function at(int i) which returns that element. I need to change that to fix this problem?
They both need to be fixed. The const versions should return const references and the non-const versions should return non-const references.
Topic archived. No new replies allowed.