开发者

I get syntax error and missing type

开发者 https://www.devze.com 2023-01-30 15:57 出处:网络
#include \"MyArrayList.h\" MyArrayList::MyArrayList() { size = 0; } NODE* MyArrayList::list_create(void *data)
 #include "MyArrayList.h"

 MyArrayList::MyArrayList()
 {
size = 0;   
 }

 NODE* MyArrayList::list_create(void *data)
 {
NODE *node;
if(!(node=malloc(sizeof(NODE))))
    return (NODE*)NULL;
node->data=data;
node->next=NULL;
return node;
}

NODE *MyArrayList::list_add(NODE *node, void *data)
{
    NODE *newnode;
    newn开发者_StackOverflow中文版ode=list_create(data);
    newnode->next = node->next;
    node->next = newnode;
return newnode;
}

On

  NODE* MyArrayList::list_create(void *data)
 {


 NODE *MyArrayList::list_add(NODE *node, void *data)
{

myarraylist.cpp(8): error C2143: syntax error : missing ';' before '*'

myarraylist.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

myarraylist.cpp(8): error C2065: 'data' : undeclared identifier

myarraylist.cpp(8): error C2761: 'MyArrayList::NODE *MyArrayList::list_create(void *)' : member function redeclaration not allowed

myarraylist.cpp(8): fatal error C1903: unable to recover from previous error(s); stopping compilation

Its a linked list


You shouldn't use NULL in C++, but 0. And you don't need to cast a null pointer: any pointer type can point at null. And you shouldn't use void pointers in C++ if you can avoid these. In fact, you shouldn't use raw pointers if you can avoid these. And you shouldn't use malloc() in C++ if you can use new. That said, you must cast the return value of malloc() if the left-hand-side of the assignment is not a void pointer:

node = (NODE*)malloc(sizeof(NODE))
0

精彩评论

暂无评论...
验证码 换一张
取 消