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];
    }
}
iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • _"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?? – Lightness Races in Orbit Feb 15 '16 at 19:37
  • 1
    You haven't defined an [overload of the subscript `operator[]`](http://en.cppreference.com/w/cpp/language/operator_member_access) for your class, for one thing. – wkl Feb 15 '16 at 19:43
  • `DynamicArray dArray();` -> `DynamicArray dArray;`, and `*array = new int[size];` -> `array = new int[size];` – crashmstr Feb 15 '16 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. – iksemyonov Feb 15 '16 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. – iksemyonov Feb 15 '16 at 20:01
  • @iksemyonov: I wasn't actually talking to you, so... – Lightness Races in Orbit Feb 15 '16 at 20:03
  • @crashmstr I've included your comment in my answer below, if that's alright. – iksemyonov Feb 15 '16 at 20:03
  • @PreferenceBean yes, as I said, it's totally a cultural thing. so let's skip it then. PS yes i sometimes get into stories defending others who probably even don't need it, true. – iksemyonov Feb 15 '16 at 20:04
  • @PreferenceBean Maybe I should clarify I live in a house with 4 CSE majors. Me and one other SWE, an IT, and CS majors. Me and the other SWE are taking it together and are both stuck. Not like we havent been trying to fix our program before I came here. – Alex Dushane Feb 21 '16 at 23:59
  • @AlexDushane: Haha okay that makes more sense :P – Lightness Races in Orbit Feb 22 '16 at 00:43
  • exactly what i was thinking. @PreferenceBean I may be mistaken but "house" != "home", while "family" == "home" in English. Language subtleties matter sometimes. – iksemyonov Feb 22 '16 at 10:14
  • @iksemyonov: Yes they do and if someone's a student (admittedly a _school_ student was my accidental and wrong assumption) then "in the house" suggests the family home and "anyone in the house" suggests family members. Source: native speaker. Thanks. (Not sure why you're still butting in to this?) – Lightness Races in Orbit Feb 22 '16 at 10:49
  • Well, maybe to learn some English from a native speaker? – iksemyonov Feb 22 '16 at 11:14

1 Answers1

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.

iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • Thank you so much. I am very unfamiliar with C++ and having to dive right in. – Alex Dushane Feb 22 '16 at 00:05
  • You welcome. Read a good book on C++ from the [Definitive C++ book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) - C++ is hard if possible at all to learn with trial and error. – iksemyonov Feb 22 '16 at 10:18
  • My professor does a wonderful job explaining the concept and totally skips over implementation so this may be what I need. – Alex Dushane Feb 22 '16 at 18:44
  • 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. – iksemyonov Feb 22 '16 at 19:33