【发布时间】:2014-11-21 04:02:44
【问题描述】:
问题: 当我尝试按索引分配 IntArray 对象时,出现以下错误:
“表达式不可赋值。”
该错误是由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++ 中),其中重载了以下运算符:
- "[ ]" : 允许索引范围检查
- "=" : 允许数组赋值
- "+" : 允许将两个数组的总和分配给第三个数组
- "+=" : 允许将两个数组的总和分配给第一个数组
- "
该程序还包括功能:
- setName : 设置 IntArray 对象的名称
- getName : 返回 IntArray 对象的名称
- low : 返回最小的合法索引
- high : 返回最大的合法索引
- length : 返回元素个数
驱动程序(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++ 程序,所以我想了解我的错误。衷心感谢您的帮助。
【问题讨论】:
-
您的
operator=太复杂了。它可以写得比你尝试的要简单得多。此外,您的operator+可以写成只调用operator +=。
标签: c++ arrays operator-overloading