I'm having problems with the code from my text book. I need modify the class so it throws an exception instead. The problem is that I get errors from the original code from the book. Any help on how to fix it would be great. Thanks in advance!

ERROR: Error 1 error C2511: 'int &IntArray::operator [](const int)' : overloaded member function not found in 'IntArray'

ERROR(ish): 2 2 IntelliSense: declaration is incompatible with "int &IntArray::operator[](const int &)" (declared at line 18 of "c:\users\projects\intarray\intarray\IntArray.h")c:\users\projects\intarray\intarray\intarray.cpp 58 16 IntArray


.H file

#ifndef INTARRAY_H
#define INTARRAY_H

class IntArray
{
private:
    int *aptr;                           // Pointer to the array
    int arraySize;                       // Holds the array size
    void subscriptError();           // Handles invalid subscripts
public:
    IntArray(int);                       // Constructor
    IntArray(const IntArray &);  // Copy constructor
    ~IntArray();                         // Destructor
    
    int size() const                     // Returns the array size
        { return arraySize; }

    int &operator[](const int &);    // Overloaded [] operator
};
#endif

.cpp file

#include <iostream>
#include <cstdlib>  // For the exit function
#include "IntArray.h"
using namespace std;

//*******************************************************
// Constructor for IntArray class. Sets the size of the *
// array and allocates memory for it.                   *
//*******************************************************

IntArray::IntArray(int s)
{
    arraySize = s;
    aptr = new int [s];
    for (int count = 0; count < arraySize; count++)
        *(aptr + count) = 0;
}

//******************************************************
// Copy Constructor for IntArray class.                *
//******************************************************

IntArray::IntArray(const IntArray &obj)
{
    arraySize = obj.arraySize;
    aptr = new int [arraySize];
    for(int count = 0; count < arraySize; count++)
        *(aptr + count) = *(obj.aptr + count);
}

//******************************************************
// Destructor for IntArray class.                      *
//******************************************************

IntArray::~IntArray()
{
    if (arraySize > 0)
        delete [] aptr;
}

//***********************************************************
// subscriptError function. Displays an error message and   *
// terminates the program when a subscript is out of range. *
//***********************************************************

void IntArray::subscriptError()
{
    cout << "ERROR: Subscript out of range.\n";
    exit(0);
}

//*******************************************************
// Overloaded [] operator. The argument is a subscript. *
// This function returns a reference to the element     *
// in the array indexed by the subscript.               *
//*******************************************************

int &IntArray::operator[](const int C)
{
    if (C < 0 || C >= arraySize)
        subscriptError();
    return aptr[C];
}

main.cpp file

#include <iostream>
#include "IntArray.h"
using namespace std;

int main()
{
    const int SIZE = 10;  // Array size

    // Define an IntArray with 10 elements.
    IntArray table(SIZE);

    // Store values in the array.
    for (int x = 0; x < SIZE; x++)
        table[x] = x;

    // Display the values in the array.
    for (int x = 0; x < SIZE; x++)
        cout << table[x] << " ";
    cout << endl;

    // Attempt to use an invalid subscript...
    cout << "Now attempting to use an invalid subscript.\n";
    table[SIZE + 1] = 0;
    return 0;
}

Check line 58 of the CPP file versus what you have in the header. They don't match up.

I'm no good with pointers, I need to practice with them more. What should/could I do to those lines to get it to work? Thanks

In line 18 of the header file, the function's signature states that you are passing in a reference to a constant int. In line 58 of the CPP file, the signature is just a constant int. Modify line 58 to reflect the reference variable (i.e., add an & to it).

Alright, will try that. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.