user3062299
user3062299

Reputation: 55

C++ Array Class Definition and Implementation, Not Declared in Scope?

So I've been working on this all day and it almost works, but when I compile it with GCC it gives me three error messages:

arrayClassDriver.cpp:11: error: 'arrayClass' was not declared in this scope
arrayClassDriver.cpp:11: error: expected ';' before 'test'
arrayClassDriver.cpp:13: error: 'test' was not declared in this scope

Obviously it thinks arrayClass (the class instance) is a variable. How can I fix this?

Here's my definition of my class:

//Pre and Post Conditions
//arrayClass(int inputSize);
//Pre: A user defined int inputSize.
//Post: An array with size = inputSize has been created.

//~arrayClass();
//Pre: A dynamically created array.
//Post: The array has been deleted.

//void display();
//Pre: A dynamically created array.
//Post: List of all the values from the array.

//void resize(int newSize);
//Pre: A dynamically created array.
//Post: An array with the new size defined by the user.

//int& operator[](int location);
//Pre: None.
//Post: An operator which allows the programmer to access the array.

//int findMaxValue();
//Pre: A dynamically created array.
//Post: The max value of the array is returned.

//int findMinValue();
//Pre: A dynamically created array.
//Post: The min value of the array is returned.

//void sort();
//Pre: A dynamically created array.
//Post: A sorted array ready to be searched.

//int search(int key);
//Pre: A dynamically created array.
//Post: The sought value is returned.


#ifndef ARRAYCLASS_H
#define ARRAYCLASS_H

#include <iostream>

namespace arrayClass_Namespace
{
    class arrayClass
    {
        private:
            int *arr;
            const static int MAX_SIZE = 1000;
            int size;
        public:
            //Constructor
            arrayClass(int inputSize);
            //Destructor
            ~arrayClass();

            void display() const;
            void resize(int newSize);

            int& operator[](int);

            int findMaxValue();
            int findMinValue();
            void sort();
            int search(int key);
    };
}

#endif

Here's my implementation (I know I haven't written the code for the sort or the search yet):

#include <cassert>
#include "arrayClass.h"

arrayClass_Namespace::arrayClass::arrayClass(int inputSize)
{
    assert(inputSize < MAX_SIZE);
    size = inputSize;
    arr = new int[size];

    for (int index = 0; index < size; index++)
    {
        arr[index] = 0;
    }
}

arrayCLass_Namespace::arrayClass::~arrayClass()
{
    delete[] arr;
    arr = NULL;
}

void arrayClass_Namespace::arrayClass::display() const
{
    std::cout << "\nIndex" << "\t" << "Value";

    for (int index = 0; index < size; index++)
    {
        std::cout << index << "\t" << arr[index];
    }
}

int& arrayClass_Namespace::arrayClass::operator[](int location)
{
    assert(0 <= location && location < size);
    return arr[location];
}

int arrayClass_Namespace::arrayClass::findMaxValue()
{
    int max = 0;

    for (int index = 0; index < size; index++)
    {
        if (max < arr[index])
        {
            max = arr[index];
        }
        else if (max > arr[index])
        {
            max = max;
        }
    }
}

int arrayClass_Namespace::arrayClass::findMinValue()
{
    int min = arr[1];

    for (int index = 0; index < size; index++)
    {
        if (min > arr[index])
        {
            min = arr[index];
        }
        else if (min < arr[index])
        {
            min = min;
        }
    }
}

void arrayClass_Namespace::arrayClass::sort()
{

}

int arrayClass_Namespace::arrayClass::search(int key)
{

}

And finally, here's my driver (still need to add a few things, but I wanted to do this first):

#include <cstdlib>
#include "arrayClass.h"

int main()
{
    arrayClass test(10);

    test.display();

    return EXIT_SUCCESS;
}

Besides that, am I using the display member function correctly? It's been a little while since I've worked with classes, so any help is greatly appreciated.

Thanks in advance!

Upvotes: 0

Views: 218

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311156

You forgot to specify namespace for arrayClass in this statement

arrayClass test(10);

Shall be

arrayClass_Namespace::arrayClass test(10);

Or you could write

#include <cstdlib>
#include "arrayClass.h"

int main()
{
    using arrayClass_Namespace::arrayClass;

    arrayClass test(10);

    test.display();

    return EXIT_SUCCESS;
}

Upvotes: 1

Related Questions