1

I have created my own header file, this is how we were asked to do it, but what arguments should I use in my main program to call this header file and create an Array.

My header file looks like this:

#ifndef ARRAY_H
#define ARRAY_H

class Array {
public:

    Array(int size) : _size(0), _arr(0) {
        // Call resize to initialize oneself
        resize(size) ;
    }

    Array(const Array& other) : _size(other._size) {
        _arr = new double[other._size] ;

        // Copy elements
        for (int i=0 ; i<_size ; i++) {
            _arr[i] = other._arr[i] ;
        }
    }

    ~Array() {
        delete[] _arr ;
    }

    Array& operator=(const Array& other)
    {
        if (&other==this) return *this ;
        if (_size != other._size) {
            resize(other._size) ;
        }
        for (int i=0 ; i<_size ; i++) {
            _arr[i] = other._arr[i] ;
        }
    }

    double& operator[](int index) {
        return _arr[index] ;
    }
    const double& operator[](int index) const {
        return _arr[index] ;
    }

    int size() const { return _size ; }

    void resize(int newSize) {
        // Allocate new array
        double* newArr = new double[newSize] ;

        // Copy elements
        for (int i=0 ; i<_size ; i++) {
            newArr[i] = _arr[i] ;
        }

        // Delete old array and install new one
        if (_arr) {
            delete[] _arr ;
        }
        _size = newSize ;
        _arr = newArr ;
    }

private:
    int _size ;
    double* _arr ;
} ;

#endif
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 7
    You don't know how to use `#include`? – Some programmer dude Feb 01 '13 at 11:12
  • 3
    wait - what? you've learned to write C++ so far, but you've not learned the basics (i.e. how header file inclusion works?) Go back to chapter 1 of your book (or throw it away and get a better one!) – Nim Feb 01 '13 at 11:12
  • you don't call headers; you include headers and instantiate types and call methods. – Marius Bancila Feb 01 '13 at 11:15
  • 4
    Why all the downvotes for this question? The poster is obviously new to C++. Isn't it valid to ask a basic question on SO? Especially since it's difficult to find the answer if you don't know the keyword to google for. – Agentlien Feb 01 '13 at 11:24
  • OT: If you ever resize to a smaller array than you started with, I'd brush up on your debugging skillz, because this is going to core on you. Further, the check for `if (_arr)` in said-same function **after** using `_arr` for the source of all that out-of-bounds copying is like showing up a day after the party ends, isn't it? – WhozCraig Feb 01 '13 at 11:30

2 Answers2

2
  1. Do not write implementation of methods in .h file. There are only a few exceptions when writing code in header file is justified and your case is not one. You should move implementations to a cpp file inside your project. Read carefully: Why have header files and .cpp files in C++?
  2. If you want to use your .h file, simply write #include "your-h-filename.h". You will then be able to use defined classes, variables and functions defined in your .h file.

You might want to read this: http://www.learncpp.com/cpp-tutorial/19-header-files/

Community
  • 1
  • 1
Spook
  • 25,318
  • 18
  • 90
  • 167
  • *Do not* without a justification isn't very useful. Plus it isn't clear that you shouldn't do this in some cases. – juanchopanza Feb 01 '13 at 11:17
  • 1
    Ok, but take into consideration, that OP is a beginner. Do you *really* want to tell beginner about inline and template functions? – Spook Feb 01 '13 at 11:17
  • Given that they are a beginner, you could explain why it would be a good idea not to do this. – juanchopanza Feb 01 '13 at 11:21
  • Again: I might, but if OP has a problem in including his own header file, I bet, that he as well may have problems in understanding compilation units, static libraries, linker errors etc. It's clear, that some basic knowledge is required here and SO is not a tutorial. I gave him a link with tutorial to header files as well. – Spook Feb 01 '13 at 11:23
  • Well, unless OP includes the implementations in the header, 2. won't work. If you tell them not to include the implementations, you need to tell them to build and link said implementations into the main. – juanchopanza Feb 01 '13 at 11:36
1

In any .cpp file where you want to use this class, assuming "Array.h" is the relative path to the header file from your .cpp file, put the line:

#include "Array.h"

Near the top of the file, before any function or type declarations. This ensures that the code in your array file is treated as if it were written at that point in that .cpp file.

As a further note, you usually want to split the definition of your class methods into a separate .cpp file (presumably Array.cpp). For example, for resize the definition in your header file should be changed to simply:

void resize(int newSize);

and the full definition should be put in the .cpp file:

void Array::resize(int newSize) {
// Allocate new array
double* newArr = new double[newSize] ;

// Copy elements
for (int i=0 ; i<_size ; i++) {
  newArr[i] = _arr[i] ;
}
Agentlien
  • 4,996
  • 1
  • 16
  • 27