嘿,我正在为 C++ 制作一个有序 ArrayList,用于插入数字并用该数字替换 ArrayList 中的空格的函数之一不起作用。我收到一条错误消息:错误 C2065:index_undeclared 标识符;
这是我的代码:
有序数组.cpp
#include"OrderedArray.h"//To allow the use of Array.h Header file.
#include<iostream>
using namespace std;// To allow the use of the standard library.
int main()//Main method for OrederedArray.cpp.
{
//Creating an array with the Datatype in from the template.
OrderedArray<int> intArray(10);
//Filling the Array(intArray)with elements from 1-10.
intArray[0] = 1;
intArray[1] = 200;
intArray[2] = 323;
intArray[3] = 976;
intArray[4] = 545;
intArray[5] = 767;
intArray[6] = 234;
intArray[7] = 123;
intArray[8] = 456;
intArray[9] = 789;
intArray.printArray();
intArray.Remove(7);
intArray.printArray();
intArray.Insert(1232,1);
intArray.printArray();
getchar();
getchar();
}
有序数组.h
//Header file for OrderedArray.cpp.
#ifndef ORDEREDARRAY_H
#define ORDEREDARRAY_H
//Template for use with the Array's data type.
template<class Datatype>
//***************************************************************************************
class OrderedArray
{
private:
//Member Variables.
int size;
int grow_size;
int num_elements;
Datatype* m_array;
//***************************************************************************************
public:
//Constructor.
OrderedArray( int p_size)
{
m_array = new Datatype[p_size];
size = p_size;
}
//***************************************************************************************
//Destructor.
~OrderedArray()
{
if( m_array!= 0 )
delete[] m_array;
m_array= 0;
}
//***************************************************************************************
//Access Operator: Syntax to allow the use of editing.
Datatype& operator[] ( int p_index)
{
return m_array[p_index];
}
//***************************************************************************************
//Functions.
void ArrayFunction( int* p_array)
{
p_array[0] = 10;
}
//Print Function: To print out all elemenst in the Array.
void printArray()
{
for(int i=0;i< size;i++)
{
cout << "Position: " <<m_array[i]<<endl;
}
}
//***************************************************************************************
//Resize Function: To resize the Array.
void Resize( int p_size)
{
Datatype* newarray = new Datatype[p_size];
if( newarray== 0 )
return;
int min;
if( p_size< size)
min = p_size;
else
min = size;
int index;
for( index = 0; index < min; index++ )
newarray[index] = m_array[index];
m_size= p_size;
if( m_array!= 0 )
delete[] m_array;
m_array= newarray;
}
//***************************************************************************************
//Remove Function: To remove an element in an Array.
void Remove( int p_index)
{
int index;
for( index = p_index+ 1; index < size; index++ )
m_array[index -1] = m_array[index];
}
//***************************************************************************************
//Size Function: To get the size of the Array.
int Size()
{
return size;
}
//***************************************************************************************
//Insert Function: To insert an element into the array between points in the index.
void Insert( Datatype p_item, int p_index)
{
for(int index = size-1; index > p_index; index—)
m_array[index] = m_array[index -1];
m_array[p_index] = p_item;
}
//***************************************************************************************
//***************************************************************************************
};
#endif