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
5
  • 7
    You don't know how to use #include? Commented Feb 1, 2013 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!) Commented Feb 1, 2013 at 11:12
  • you don't call headers; you include headers and instantiate types and call methods. Commented Feb 1, 2013 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. Commented Feb 1, 2013 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? Commented Feb 1, 2013 at 11:30

2 Answers 2

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/

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

5 Comments

Do not without a justification isn't very useful. Plus it isn't clear that you shouldn't do this in some cases.
Ok, but take into consideration, that OP is a beginner. Do you really want to tell beginner about inline and template functions?
Given that they are a beginner, you could explain why it would be a good idea not to do this.
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.
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.
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] ;
}

Comments

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.