因此,我整天都在努力工作,并且几乎可以正常工作,但是当我使用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;
}


除此之外,我是否可以正确使用显示成员函数?自从我上过课以来已经有一段时间了,因此,非常感谢您的帮助。

提前致谢!

最佳答案

您忘记在此语句中为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;
}

关于c++ - 未在范围内声明C++数组类的定义和实现?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21665750/

10-11 16:52