0

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!

7
  • arrayClass is in the arrayClass_Namespace namespace, you have to qualify it in main. Commented Feb 9, 2014 at 22:13
  • Oh! Alright, I'll test it out. Commented Feb 9, 2014 at 22:14
  • Hmmm.... Now I'm getting another error. "undefined reference to 'arrayClass_Namespace::arrayClass::~arrayClass();" and for the constructor and display function. Commented Feb 9, 2014 at 22:16
  • Make sure you compile the .cpp file that has the definitions of those functions into your program. Commented Feb 9, 2014 at 22:16
  • So I compiled all three files together, and it seems to work... Thanks guys! I guess I just assumed it would've compiled all of them together in the first place. Commented Feb 9, 2014 at 22:18

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

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.