“表达式不可分配。”
该错误是由iadrv.cpp中的以下代码行产生的:
IntArray a(10);
for(int i = a.low(); i <= a.high(); i++)
a[i] = i * 10;
我可以像这样将整个IntArray对象分配给另一个对象a = b;
,但是当引用特定的索引时,将发生“表达式不可分配”错误。
编辑:我const
从大多数函数中删除了声明,并且我不再收到“表达式不可分配”错误了。但是,setName现在给出错误:
“ ISO C ++ 11不允许从字符串文字转换为'char *'”
此错误是由iadrv.cpp中的以下代码引起的:
a.setName("a");
我写了一个IntArray类(用C ++语言编写),其中以下运算符被重载:
该程序还包括以下功能:
驱动程序(iadrv.cpp,iadrv.h)将在IntArray类(IntArray.cpp,IntArray.h)上运行测试,以确定是否所有操作符均已正确重载。
注意:对于每个数组测试数据,驱动程序将在每个数组初始化或修改后立即将数组索引乘以10并输出其内容。当程序遇到运行时错误时,应使用适当的诊断程序“模拟”停止,而不是实际停止程序。
IntArray.h
// IntArray.h
#ifndef __IntArray__IntArray__
#define __IntArray__IntArray__
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class IntArray {
private:
int a, b;
int size;
int * num;
char * name;
public:
IntArray(int start, int finish);
IntArray(int finish = 10);
IntArray(const IntArray &); //constructor copy
~IntArray();
int low() const;
int high() const;
char * getName() const;
//removed the const declaration from functions below
int & operator [] (int); //made to return int&
friend ostream & operator << (ostream &, IntArray &);
void setName(char *);
int length() const;
const IntArray & operator = (IntArray &);
const IntArray & operator + (IntArray &);
bool operator += (IntArray &);
};
#endif /* defined(__IntArray__IntArray__) */
IntArray.cpp
// IntArray.cpp
#include "IntArray.h"
#include <iostream>
#include <fstream>
using namespace std;
extern ofstream csis;
IntArray::IntArray(int start, int finish) {
if (start > finish) {
cout << "Simulating a halt.";
a = finish;
b = start;
}
else {
a = start;
b = finish;
}
size = b-a;
num = new int[size];
name = new char[1];
for (int i = 0; i < size; i++) {
num[i] = 0;
}
}
IntArray::IntArray(int finish) {
size = finish;
a = 0;
b = finish - 1;
num = new int[size];
name = new char[1];
for (int i = 0; i < size; i++) {
num[i] = 0;
}
}
IntArray::IntArray (const IntArray & right): size(right.size) {
num = new int[size];
name = new char[1];
for (int i = 0; i < size; i++) {
num[i] = right.num[i];
}
}
IntArray::~IntArray() {
delete[] num;
delete [] name;
}
int IntArray::low() const{
return a;
}
int IntArray::high() const{
return b;
}
char * IntArray::getName() const{
return name;
}
void IntArray::setName(char * n) {
name = n;
}
//removed const declarations
//made to return int&
int & IntArray::operator [] (int subscript) const{
if (subscript < a || subscript > b) {
cout << "subscript: " << subscript << endl;
cout << "Out of bound error. Simulating a halt." << endl;
return num[a];
}
return num[subscript-a];
}
int IntArray::length() const{
//b-a = size
return (b-a);
}
//removed const declarations
ostream & operator << (ostream & output, IntArray & array) {
for (int i = array.low(); i <= array.high(); i++) {
output << array.name << "[" << i << "] = " << array[i] << endl;
}
return output;
}
//removed const declarations
IntArray & IntArray::operator = (IntArray & right) {
if (length() == right.length()) {
for (int i = 0; i <= length(); i++) {
num[i] = right[right.low()+i];
}
return * this;
}
else {
delete [] num; //reclaim space
delete [] name;
size = right.length();
num = new int [size]; //space created
cout << "Different sized arrays. Simulating a hault" << endl;
}
return * this;
}
//removed const declarations
IntArray & IntArray::operator + (IntArray & right) {
int * ptr;
ptr = new int [right.length()];
if (length() == right.length()) {
for (int i = 0; i < length(); i++) {
ptr[i] = num[i] + right[right.low()+i];
}
}
return * this;
}
//removed const declarations
bool IntArray::operator += (IntArray & right) {
if (length() == right.length()) {
for (int i = 0; i <= right.length(); i++) {
num[i] += right[right.low()+i];
}
return true;
}
cout << "Could not add the sum of the arrays into first array. Simulating a halt." << endl;
return false;
}
iadrv.h
// iadrv.h
#ifndef p6_iadrv_h
#define p6_iadrv_h
#include "intarray.h"
int main();
void test1();
void wait();
#endif
iadrv.cpp
// iadrv.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include "iadrv.h"
using namespace std;
ofstream csis;
int main() {
csis.open("csis.dat");
test1();
csis.close();
}
void test1() {
system("clear");
cout << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
csis << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
IntArray a(10);
for(int i = a.low(); i <= a.high(); i++)
a[i] = i * 10;
a.setName("a");
cout << a << endl;
csis << a << endl;
wait();
}
免责声明:该程序是作为学校作业编写的,已经被上交评分。这是我的第一个C ++程序,所以我想了解我的错误。真诚的感谢您的帮助。
您已如下定义了运算符[]:
const int operator [] (int) const;
第二个“ const”表示在该方法内部您无法修改对象。
因此,它仅适用于获取值,而不适用于设置它们。
尝试将其删除,它应该可以工作。
编辑:指向了布莱恩·陈,您还需要返回一个引用和非常量,像这样:
int& operator [] (int subscript)
现在,深入了解您的代码,这还不够,因为您具有以下方法:
ostream & operator << (ostream & output, const IntArray & array) {
for (int i = array.low(); i <= array.high(); i++) {
output << array.name << "[" << i << "] = " << array[i] << endl;
}
return output;
}
看起来您的operator []需要在非const IntArray上工作,但是在该方法中,变量“ array”是const,因此您需要重写更多代码。
此外,寻找与其他运算符相同的问题,请记住:仅当您不打算从该方法内部修改对象时,才创建方法“ const”,并且仅当您要创建参数“ const”时,才创建该方法不打算修改该参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句