gpt4 book ai didi

c++ - ArrayList 无法返回结构

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:05 25 4
gpt4 key购买 nike

大家好,我一直在使用我的数组列表,现在已经使用了一段时间,我只是尝试在这个程序的数组列表中使用结构类型:

#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

当我尝试运行它时出现此错误:error C2446: ':' : no conversion from 'int' to 'main::Params' on line 86我完全坚持这个任何人有任何建议我不知道该怎么做我认为这可能是因为模板,因为它说它必须返回一个 int 但事实并非如此,因为它适用于其他类型所以也许它只是结构。注:第86行是get函数

最佳答案

如果 TParams 那么这不起作用:

inline T get(int i){
return length != 0 ? a1[i] : NULL;//returns a[i] if the length is not 0
}

NULL0 的宏,Params 没有接受0 的构造函数。

在 C++ 中没有“空对象”这样的东西(其他一些语言有这个概念,C++ 没有)。

您必须重新设计此函数以返回特定值(例如 T(),默认构造的 T),或抛出异常。

在执行 a1[i] 之前,您还应该检查 i 是否在数组的范围内;请注意,只有当 T 是“普通旧数据”类型时,使用 malloc 函数族才有效,因为它不调用任何构造函数。例如,此代码将无法用于包含 std::string 的任何内容。

关于c++ - ArrayList 无法返回结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25987802/

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