gpt4 book ai didi

c++ - Ordered ArrayList 的 Insert 方法出错

转载 作者:行者123 更新时间:2023-11-30 02:03:39 25 4
gpt4 key购买 nike

嘿,我正在为 C++ 创建一个 Ordered 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

最佳答案

你这样说:

    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;
}

什么时候应该是这样的:

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;
}

请注意修改后的代码段中 for 语句末尾的 index--

关于c++ - Ordered ArrayList 的 Insert 方法出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com