2014-09-23 62 views
0

您好所有我一直与我的数组列表,并使用了一段时间了,现在我只是想在我的数组列表在这个程序中使用结构类型:的ArrayList无法返回一个struct

#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:“:”:没有转换,从“诠释”到“主:: PARAMS”上线86 我完全卡在这个任何人有任何建议,我不知道该怎么办我认为这可能是因为t他的模板是因为它说它必须返回一个int,但这不是真的,因为它可以与其他类型一起工作,所以也许它只是结构。注意:第86行是get函数

+1

哪条线是86? – user2079303 2014-09-23 05:56:32

+0

对不起,第86行是获取函数 – AnonymousUser 2014-09-23 05:57:14

回答

1

NULL是一个指针类型。你的T不是一个指针。您不能返回NULL而不是T。您需要找到另一种方式来表明不存在这样的条目。

//      T  not T 
return length != 0 ? a1[i] : NULL; 
+0

等待null被定义为0,所以我没有得到如何这是一个指针类型 – AnonymousUser 2014-09-23 05:59:55

+0

噢好吧谢谢你的解释 – AnonymousUser 2014-09-23 06:01:16

+0

当我跳转到定义它的定义为((void *)0 ),但无论如何......即使它是标准的0,即使* *不是你的类型“T”。你需要返回一个'T'类型。 – nvoigt 2014-09-23 06:01:26

3

如果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++中,不存在“null对象”(某些其他语言具有该概念,C++没有)。

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

在做a1[i]之前,您还应该检查i是否在数组范围内;并且请注意,使用malloc函数系列仅适用于T是“普通旧数据”类型,因为它不调用任何构造函数。例如,该代码将不可用于包含std::string的任何内容。

1

在你的get()函数中NULL等于0,这是一个int。编译器说在某些情况下get()可能会返回一个int而不是类型Params。您可能想要添加一个断言或异常,以确保在返回数组值之前该大小足够大。