0
所以我一直對這個整天和它差不多的作品,但是當我用GCC編譯它,它給了我三個錯誤信息:C++數組類的定義和實現,未在範圍中聲明?
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_Namespace'命名空間,你必須在'main'中限定它。 – user3286380
哦!好的,我會測試一下。 – user3062299
嗯....現在我收到另一個錯誤。 「未定義的引用'arrayClass_Namespace :: arrayClass ::〜arrayClass();」併爲構造函數和顯示功能。 – user3062299