C ++数组类定义和实现,未在范围内声明?

时间:2014-02-09 22:12:17

标签: c++ arrays class

所以我整天都在努力,它几乎可以工作,但是当我用GCC编译它时,它给了我三条错误信息:

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

显然它认为arrayClass(类实例)是一个变量。我该如何解决这个问题?

这是我对班级的定义:

//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

这是我的实现(我知道我还没有编写排序或搜索的代码):

#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)
{

}

最后,这是我的驱动程序(仍然需要添加一些东西,但我想先做这个):

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

int main()
{
    arrayClass test(10);

    test.display();

    return EXIT_SUCCESS;
}

除此之外,我是否正确使用了显示成员功能?自从我上课以来已经有一段时间了,所以非常感谢任何帮助。

提前致谢!

1 个答案:

答案 0 :(得分:1)

您忘记在此声明中为arrayClass指定命名空间

arrayClass test(10);

应该

arrayClass_Namespace::arrayClass test(10);

或者你可以写

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

int main()
{
    using arrayClass_Namespace::arrayClass;

    arrayClass test(10);

    test.display();

    return EXIT_SUCCESS;
}