所以,我试图做一个程序来概括添加,删除,显示双链表。但我在添加部分遇到了问题。编译时遇到“对insertNodeBeggining(List *,void *)的未定义引用”。有什么问题?
的main.cpp
#include <iostream>
#include <cstring>
#include "methods.h"
using namespace std;
int main()
{
List *head=createList();
void *p=NULL;
insertNodeBeggining(head,p);
return 0;
}
methods.cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
typedef struct nodeType{
nodeType *next;
nodeType *prev;
void* data;
} NodeT;
typedef struct Lists{
NodeT *first;
NodeT *last;
} List;
List *createList()
{
return (List*)malloc(sizeof(List));
}
void insertNodeBeggining(List *head, void *value)
{
NodeT *nou=(NodeT*)malloc(sizeof(NodeT));
nou->data=value;
if(head->first==NULL)
{
head->first=nou;
head->last=nou;
nou->next=NULL;
nou->prev=NULL;
}
else
{
nou->next=head->first;
head->first->prev=nou;
nou->prev=NULL;
head->first=nou;
}
}
methods.h
#ifndef METHODS_H_INCLUDED
#define METHODS_H_INCLUDED
typedef struct NodeT;
typedef struct List;
List *createList();
void insertNodeBeggining(List *head, void *value);
#endif // METHODS_H_INCLUDED
答案 0 :(得分:0)
似乎问题是编译器将两个名称List,一个在header中,另一个在methods.cpp中,作为两个不同的名称。
虽然编译器可能不会发出错误
,但标头中的typedef无效typedef struct List;
您应该排除这些定义
typedef struct nodeType{
nodeType *next;
nodeType *prev;
void* data;
} NodeT;
typedef struct Lists{
NodeT *first;
NodeT *last;
}List;
来自metods.cpp的并将它们包含在标题中而不是typedef(s)中。
methods.cpp必须包含这个新标题。
答案 1 :(得分:0)
基本上,不能通过typedef
完成不完整的类型。
因此,实施文件中的typedef
- 编辑List
不被视为已宣布的前向声明不完整List
的定义。
要解决此问题,请替换C样式
typedef struct Lists{
NodeT *first;
NodeT *last;
} List;
使用C ++风格
struct List
{
NodeT* first;
NodeT* last;
};
免责声明:我没有通过编译器运行它。