是否可以通过带有运算符重载的索引来分配用户定义的数组?-C ++

cjan92127


问题: 当我尝试通过索引分配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对象的名称
  • 低:返回最小的合法索引
  • 高:返回最大的合法指数
  • 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 ++程序,所以我想了解我的错误。真诚的感谢您的帮助。

丹尼尔

您已如下定义了运算符[]:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过带有类型通知的宏在C中重载运算符

来自分类Dev

带有原始类型的C ++运算符重载

来自分类Dev

重载元组索引运算符-C ++

来自分类Dev

C ++重载数组运算符

来自分类Dev

C ++ 20概念要求运算符重载与用户定义模板运算符重载功能相结合

来自分类Dev

XCode中的C ++错误:定义运算符*“没有可行的重载运算符* =“,而* =已经过测试并且可以正常运行

来自分类Dev

用户定义类的运算符重载在C ++中不起作用

来自分类Dev

用于Char分配的重载运算符[]-C ++

来自分类Dev

如何在C ++中重载索引运算符?

来自分类Dev

C ++运算符重载++

来自分类Dev

C ++-重载[]运算符

来自分类Dev

C ++运算符重载

来自分类Dev

C ++“ *”运算符重载

来自分类Dev

C ++运算符>>重载

来自分类Dev

()运算符重载c ++

来自分类Dev

C ++运算符重载[]

来自分类Dev

C ++运算符重载?

来自分类Dev

C ++运算符重载

来自分类Dev

C ++是否可以重载右值引用的一元减运算符?

来自分类Dev

是否可以在C ++中重载运算符的关联性?

来自分类Dev

是否可以重载C ++ 11新运算符来创建智能指针?

来自分类Dev

是否可以重载C ++ 11新运算符来创建智能指针?

来自分类Dev

C ++自定义结构化数组下标运算符如何重载?

来自分类Dev

C ++中的运算符重载带有整数和对象

来自分类Dev

在C ++中使用带有const类型输入参数的函数内部使用运算符重载

来自分类Dev

C ++运算符[]通过模板访问boost :: variant的重载

来自分类Dev

C ++运算符重载还是重新定义?

来自分类Dev

有什么办法可以在C#中重载+ =运算符

来自分类Dev

为什么没有用户定义的运算符的类中的c样式数组=支持深度复制

Related 相关文章

热门标签

归档