0

i'm working on a array header from base definition of an array to create an array of any type with this header,so i created a array class with functions and constructors. this is my code so far:

#include <iostream>
#define newline "\n"
class Arr
{
public:
    typedef float T;
public:
    Arr(int size);
    Arr(int size, T fill);
    T get(unsigned index) const;
    void set(unsigned index, T newvalue);
    unsigned Size() const;
    unsigned SIZE;
    void Print();
private:
};
Arr::Arr(int size,T fill)
{
    SIZE = size;
    T *pointer;
    for (int i = 0; i < size; i++)
    {
        *pointer = fill;
        pointer++;
    }
}
void Arr::set(unsigned index, T newvalue)
{
    T *pointer;
    pointer = 0;
    for (unsigned i = 0; i < index; i++)
    {
        pointer++;
    }
    *pointer = newvalue;
}
void Arr::Print()
{
    T *pointer;
    pointer = 0;
    for (unsigned i = 0; i < SIZE; i++)
    {
        std::cout << *pointer << newline;
        pointer++;
    }
}

i know that my pointer point to nothing,as my question is my pointer should point to what to make this code work correctly?! any time i point it to 0 after debug it crashes! thanks...!

  • 2
    Your code is too wrong. [Read a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). If you have questions about something the book is trying to teach, you can ask them here. Once you are fairly confident on the book material, your code should be less wrong. – Benjamin Lindley Jan 08 '14 at 18:13
  • You have not allocated memory T at `T *pointer;` in Arr constructors. Moreover even if you did, you are not keeping track of it anywhere and is only available in method scope not at the class level. – Abhinav Jan 08 '14 at 18:14
  • Probably a private array, or you could scrap the whole class and just use an std::vector – IdeaHat Jan 08 '14 at 18:14

3 Answers3

0

You must make a data member of the class that will point to the allocated memory for the array. Also you need to define a copy constructor, the copy assignment operator and the destructor. Also it would be better that type of parameter size of constructors coinsides with the type of data member SIZE, I do not understand why this variable is written in capital letters.

ALso there is no any sense to make the data member SIZE and the function Size() public. if SIZE is public it can be changed by the user at any moment.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Pointers are tricky part of c++.

Here is a good link to get you started http://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations

The reason your code doesn't work is a memory block for the array pointed to by the pointer is not allocated. You have to use the predecessor new in-order to achieve that.

Here an example

int size;
T arr;
T* ptr_2_arr;
ptr_2_arr = new T[size];

To retrieve elements of the array you can loop the array using a for loop

*ptr_2_arr[i];

hope this helps.

Post the problem statement if you need more detail

Henok G.
  • 49
  • 5
0

Make sure you specify the size of the array in your constructor.

SIZE = size;

pointer = new T[size]; //this is where I see an issue. Specify the size of your array. 

 for (int i = 0; i < size; i++)
    {
        *(pointer + i) = fill; //This is fine but you are filling up the array with only one number, fill. Nothing wrong with that if that is you intention. Try (*(pointer + i) = i; for i though size elements. 
    }
Henok G.
  • 49
  • 5