tags:

views:

114

answers:

4

Hi! This semester in university I have a class called Data Structures, and the professor allowed the students to choose their favourite language. As I want to be a game programmer, and I can't take Java anymore, I chose C++ ... but now I'm stuck with lack of knowledge in this language. I have to do the following: create a SuperArray, which is like a Delphi array (you can choose the starting and ending index of it). My code is as follows:

main.cpp

#include <iostream>
#include "SuperArray.h"    

using namespace std;

int main(int argc, char** argv) 
{
    int start, end;
    cout << "Starting index" << endl;
    cin >> start;
    cout << "Ending index:" << endl;
    cin >> end;
    SuperArray array = new SuperArray(start,end); 
}

superarray.h

#ifndef _SUPERARRAY_H
#define _SUPERARRAY_H

class SuperArray
{
public:
    SuperArray(int start, int end);
    void add(int index,int value);
    int get(int index);
    int getLength();
private:
    int start, end, length;
    int *array;

};

#endif  /* _SUPERARRAY_H */

superarray.cpp

#include "SuperArray.h"

SuperArray::SuperArray(int start, int end)
{
    if(start < end)
    {
        this->start = start;
        this->end = end;
        this->length = (end - start) + 1;
        this->array = new int[this->length];
    }
}

void SuperArray::add(int index, int value)
{
    this->array[index-this->start] = value;
}

int SuperArray::get(int index)
{
    return this->array[index-this->start];
}

When I try to compile this code, I have the following error:

error: conversion from `SuperArray*' to non-scalar type `SuperArray' requested

What should I do?

+1  A: 

array should be a pointer to be allocated on the heap. Change its definition to:

SuperArray* array = new SuperArray(start,end); 

You can get rid of the pointer by allocating the array on the local stack:

SuperArray array(start,end); 

A stack object is enough if you don't intend to pass share the object across modules.

Vijay Mathew
Should explain why they're different!
Arafangion
+5  A: 

Unlike Java, in C++ you don't need to use the new keyword to create objects. In Java, all objects are stored on the heap (free store) and can only be accessed via references.

In C++, objects can be value types. You can declare them directly on the stack, e.g.

SuperArray array(start, end);

And you can call methods like:

array.get(1);

This object will be automatically destroyed when array goes out of scope. If you want to manage the lifetime of the array object manually, you can optionally create it on the heap using new, but then you have to refer to it with a pointer:

SuperArray* array = new SuperArray(start, end);

Now, you must call methods like this:

array->get(i);

since array in this case is a pointer to a SuperArray and not a SuperArray itself (and a pointer has no get method of its own). The -> operator means to use the . operator on the pointed-to object.

In this case, the object pointed to by the array pointer will continue to exist until you call delete array; If you fail to explicitly delete the object, it will never be deallocated (C++ has no garbage collector!) and you will have a memory leak.

Note that C++ has things called "pointers" and things called "references". A Java "reference" has some of the properties of both of these things and is not directly equivalent to either. A good introductory text on C++ should explain the difference between these.

Tyler McHenry
+1  A: 

At least as a starting point (and probably permanently, unless the prof made me change it) I'd use an std::vector to manage the storage, and just write a front-end that manages the offsets to the indices as needed.

Edit: if I was forced to change that, I'd still study std::vector's interface, and imitate it as much as possible, so nearly the only change would be the (possibly) non-zero lower bound on the index. I'd also study how it manages memory, and imitate that as well. Finally, I'd segment things about the same way -- i.e., have one class that just manages a vector with a fixed lower bound of zero, and a second class that just deals with the offset to indices necessary for a non-zero lower bound.

Jerry Coffin
He wouldnt allow that. The meaning of this class is to build the data structures from the ground, not using STL or anything that is non primitive.
Bruno
A: 

You add function seems strange to me, you really need to check the bounds, I would expect something more like this: (This might not be legal c++, it has been about 10 years and I don't have a compiler handy to test... treat it as pseudo code.)

void SuperArray::add(int index, int value)
{
    if ((index >= start) && (index <= end)
    {
       array[index - start] = value;
    }
    else
      throw(new OutOfBoundsException);      
}
Hogan
I know I have to check the bounds, just didn't know how to throw exceptions in C++. Thanks!
Bruno
"throw in c++" on google returned this > http://msdn.microsoft.com/en-us/library/6dekhbbc(VS.80).aspx
Hogan