1

Below is my current code for my latest assignment and I cannot figure out what the problem is printing the array. Forgive me for the crappy code, in my class we were thrown into C++ and none of us have ever used it before so it may be a simple mistake but no one in the house can help me.

Header file DynamicArray.h:

//
// DynamicArray.h

///#include <rpcndr.h>

#ifndef DYNAMIC_DYNAMICARRAY_H
#define DYNAMIC_DYNAMICARRAY_H

#endif //DYNAMIC_DYNAMICARRAY_H

//      union
//      intersection
//      relative complement
//      insertion - if the element is already in the set, then nothing happens
//      deletion - if the element is not in the set, then nothing happens
//      query to check whether an element is in a set
//      query to find the number of number of elements in a set
//      display the set
//destructor
//       copy constructor
// ***********************************overloading assignment operator***************************************************
class DynamicArray{
public:
    DynamicArray(int size);
    DynamicArray(const DynamicArray &original, int Size);
///     DynamicArray(int Size);
    ~DynamicArray();

    void Union();
    void Intersection();
    void Complement();
    int Insert(int position, int entry, int size);
    int Delete(int position, int entry, int size);
    bool Qelement(int size, int entry);
    int Qset(int size);


    int size = 20;
    int *array;
};

    //
// 
//

Source file DynamicA.cpp- here I define the constructors and member functions:

//
// DynamicA.cpp
//

//Union();
//Intersection();
//Complement();
//Insert();
//Delete();
//Qelement();
//Qset();
#include <iostream>
#include "DynamicArray.h"
using namespace std;

DynamicArray::DynamicArray(int &size = 30){
    size = 20;
    *array = new int[size];
    for(int i = 0; i < size; i++){
        array[i] = 0;
    };
}

///  DynamicArray::DynamicArray(int Size) {
///
///    }

DynamicArray::DynamicArray(const DynamicArray &original, int size) {
    size  = original.size;
    array = new int[size];
    for (int i = 0; i < size; i++) {
        array[i] = original.array[i];
    }
}

DynamicArray::~DynamicArray(){
    delete[] array;
}

void DynamicArray::Union(){

}

void DynamicArray::Intersection() {

}

void DynamicArray::Complement(){

}

int DynamicArray::Insert(int position, int entry, int size) {
    if(!Qelement()){
        for(int i = size+1; i > position+1; i--){
            array[i] = array[i-1];
        }
        array[position] = entry;
    }
}

int DynamicArray::Delete(int position, int entry, int size){
    for(int i = 0; i < size; i++){
        if(array[i] == entry) {
            for(int x = i; x < size; i++){
                array[x] = array[x+1];
            }
            size--;
        }
    }
}

bool DynamicArray::Qelement(int size, int entry) {
    for(int i = 0; i < size; i++){
        if(array[i] == entry){
            return true;
        }
    }
}

int DynamicArray::Qset(int size){
    return size;
}

main.cpp - this is where my issue is. The error I continue to receive is that dArray is not an array.

//main.cpp
    #include <iostream>
//#include <DynamicArray.h>
#include "DynamicArray.h"
//#include "DynamicA.cpp"

//using namespace std;

int main() {
    DynamicArray dArray();
    for(int i = 0; i < array; i++) {
        cout << dArray[i];
    }
}
13
  • "it may be a simple mistake but no one in the house can help me" What about a library? I don't think anyone ever really expected your mum and dad to be able to do your programming research for you?? Commented Feb 15, 2016 at 19:37
  • 1
    You haven't defined an overload of the subscript operator[] for your class, for one thing. Commented Feb 15, 2016 at 19:43
  • DynamicArray dArray(); -> DynamicArray dArray;, and *array = new int[size]; -> array = new int[size]; Commented Feb 15, 2016 at 19:51
  • @PreferenceBean maybe it's my Eastern background, but please with all due respect, can we refrain from mentioning parents in those tones? I understand your feelings but as for me, my parents helped me a great deal with PC's and programming / algorithms back in the days. Commented Feb 15, 2016 at 20:00
  • @PreferenceBean ok we probably won't understand each other one regarding what I've said. I meant your very first comment above. I agree that OP might show more initiative and your advice is about that, but the wording put me off, to be honest. Commented Feb 15, 2016 at 20:01

1 Answer 1

3

Your class DynamicArray is not an array, the compiler has it right. It's just a class you've defined. For your code to work, you need to overload DynamicArray::operator[](int), for example, like so:

#include <cassert>
int DynamicArray::operator[](int idx)
{
    assert(idx < size);
    return array[i];
}

The reason is that operator[] is only defined for the built-in array type, where it has an established meaning and understood by the compiler. But you have defined an arbitrary class, and your understanding that it is an array is only your understanding, i.e. an assumption, which in no way is perceived by the compiler, so to say.

Now, let me point this one out before you run into issues caused by that mistake: the fields size and array must be private or at least protected! Read up on encapsulation in C++, as well as the other two or three founding principles of this language. You may wonder how to access the size of the array form the outside given this change, well that's where the so-called getter-setter methods come into play. Define int DynamicArray::size() const { return size; } to let the array tell its size to its clients.

Now you can use the previously defined operator[](int) with int size():

DynamicArray myArray(5);
for(int i = 0; i < myArray.size(); ++i)
    std::cout << myArray[i] << " ";

Other errors: two pointed out by@crashmstr: *array = new int[size]; should be array = new int[size]; and DynamicArray myArray(); isn't going to build, since this calls the undefined default constructor.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. I am very unfamiliar with C++ and having to dive right in.
You welcome. Read a good book on C++ from the Definitive C++ book list - C++ is hard if possible at all to learn with trial and error.
My professor does a wonderful job explaining the concept and totally skips over implementation so this may be what I need.
Happens, indeed. However, I'd call operator overloading a "concept" as well, and an important one at that, so maybe you're in an early part of your course yet, and weren't expected to use the [] syntax. Still, I feel some mismatch here, or maybe your professor skips over certain parts to encourage you to discover more on your own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.