大家好,我一直在使用我的数组列表,并且已经使用了一段时间了,我只是尝试在我的数组列表中使用结构类型:
#include "stdio.h"
#include "Header.h"
int main(int argc, char**argv)
{
struct Params
{
int i;
int j;
};
Params p2;
p2.i = 2;
p2.j = 3;
List::ArrayList<Params> p = List::ArrayList<Params>();
p.add(p2);
printf("i: %d, j: %d", p.get(0).i, p.get(0).j);
return 0;
}
这是我的ArrayList程序:
#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_
#include <iostream>
using namespace std;
namespace List
{
template <class T>//template - allows the user to choose what type to store in the list
class ArrayList
{
private://private variables
int length = 1;//sets the length to 1
T *a1 = (T*)calloc(length, sizeof(T));//allocates enough memory for 1 item
public://public functions
ArrayList()
{//constructor
//does nothing but you probably already knew that
}
~ArrayList(){//destructor
//frees up the memory that a1 is pointing to
free(a1);
}
inline void add(T item){//adds an item to the list
if (length != 0)
{//if length is not equal to 0
a1 = (T*)realloc(a1, length*sizeof(T));//re-allocates memory
a1[length - 1] = item;//adds item to the end of the list
length++;//adds 1 to the length
}
else
{//else if it is equal to 0
a1[length] = item;//adds item to the front of the list
}
}
inline void remove(T item){
if (length != 0)
{
length--;//subtracts 1 from the length
T *b = (T*)calloc(length, sizeof(T));//creates a new pointer to a memory block of size length
int idxModifier = 0;//modifies the index so it adds the items to the array in the right places
int i = 0;//counter/index
while (i < length){
if (a1[i] != item)//if the item at the index of i is not equal to item we want to delete
{
b[i] = a1[i + idxModifier];
}
else
{
idxModifier++;//adds 1 to the index modifier
b[i] = a1[i + 1];//adds the next item so we dont add the item back to the array
}
i++;//adds one to i
}
if (count != 0)
a1 = (T*)realloc(b, length*sizeof(T));
else
cout << "The item does not exist!" << endl;//lets the user know that item does not exist
free(b);//frees up the memory that b allocated at the top of the function
}
else
{
cout << "You cannot delete anything because there is nothing in the list" << endl;//lets the user know that there is nothing in the list
}
}
inline T get(int i){//gets the integer at the index of i
return length != 0 ? a1[i] : NULL;//returns a[i] if the length is not 0, if it is 0 then it returns NULL wich is defined as 0
}
inline void set(int i, T type)
{
a1[i] = type;//sets whatever is at teh index of i to type
}
inline int size(){//gets the size
return length != 0 ? length - 1 : NULL;//if the length is not equal to 0 then it returns the length-1 , if it is not 0 then it returns NULL
}
inline void print(){//prints the list
if (length != 0)
{
cout << "[";//prints the end bracket
for (int i = 0; i < size(); i++){
if (i == 0)//if the index is equal to the beginning of the list
cout << a1[0];//print the first item
else
cout << ", " << a1[i];//prints a comma before the item to seperate the items
}
cout << "]" << endl;//prints the end bracket
}
else
{
cout << "There is nothing in the array to print!" << endl;//lets the user know that the list is empty
}
}
};
};
#endif
当我尝试运行它时,我收到此错误:错误C2446:':':第86行没有从'int'转换为'main :: Params' 我完全坚持这个任何人有任何建议我不知道该怎么做我认为它可能是因为模板因为它说它必须返回一个int但是这不是真的因为它适用于其他类型所以也许它只是结构。注意:第86行是get函数
答案 0 :(得分:3)
如果T
为Params
,那么这不起作用:
inline T get(int i){
return length != 0 ? a1[i] : NULL;//returns a[i] if the length is not 0
}
NULL
是0
的宏,Params
没有接受0
的构造函数。
在C ++中,没有&#34; null对象&#34; (其他一些语言都有这个概念,C ++没有这个概念)。
您必须重新设计此函数,以返回特定值(例如T()
,默认构造的T
),或者抛出异常。
在执行i
之前,您还应检查a1[i]
是否在数组的范围内;并且请注意,使用malloc
系列函数仅在T
是&#34;普通旧数据&#34;类型,因为它不调用任何构造函数。例如,对于包含std::string
的任何内容,此代码将无法使用。
答案 1 :(得分:1)
NULL
是指针类型。你的T
不是指针。您无法返回NULL
代替T
。您需要找到另一种方式来表明不存在此类条目。
// T not T
return length != 0 ? a1[i] : NULL;
答案 2 :(得分:1)
在你的get()函数中,NULL等于0,这是一个int。编译器说在某些情况下get()可能返回int而不是类型Params。您可能希望添加断言或异常以确保在返回数组值之前足够大。