有关C链表的小程序, 在VC6下面链接时报错,请高手解答

oneonone 2005-05-19 03:58:04
整个程序分三个文件组成YanLinkedList.c YanLinkedList.h 和test.c

其中前两个文件写了一个链表, test.c负责对调用并测试这个链表.结果在链接时报这样的错误:

ERROR:
________
test.obj : error LNK2001: unresolved external symbol "int __cdecl InitialLinkedList(struct _DataNode * *)" (?InitialLinkedList@@YAHPAPAU_DataNode@@@Z)
Debug/billing.exe : fatal error LNK1120: 1 unresolved externals

源代码:
/******************************************************************************
* YanLinkedList.h*/


#ifndef LINKEDLIST_FORALL_H
#define LINKEDLIST_FORALL_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*你自己的结构所在的头文件*/
/*#include "tc.h"*/

typedef char KEY[11];
typedef struct TEST_DATA
{
KEY key;
}DATA_T_X;
typedef DATA_T_X DATA_T;
int CompareKey(const KEY * pkLeft,const KEY * pkRight);
int CompareKey(const KEY * pkLeft,const KEY * pkRight)
{
return strcmp(*pkLeft,*pkRight);
}


typedef struct _DataNode
{
DATA_T data; /* 数据*/
struct _DataNode * prev; /* 指向前一个节点 */
struct _DataNode * next; /* 指向后一个节点 */
}DATANODE,*PDATANODE;

int InitialLinkedList( DATANODE **head );
int DestroyLinkedList( DATANODE *head );

DATANODE * SearchLinkedList( const DATANODE *head,const KEY * key );
DATANODE * InsertLinkedList( DATANODE ** head,const DATA_T * data );
DATANODE * RemoveNode( DATANODE ** head,const KEY * key );
int DeleteNode( DATANODE ** head,const KEY * key );

#endif /*AVLhead_FORALL_H*/





/******************************************************************************
* YanLinkedList.c*/


#include "YanLinkedList.h"

/************************************************************************/
int Compare(const DATANODE *,const KEY *);
DATANODE * MakeNode(const DATA_T * data);






int InitialLinkedList(DATANODE **head )
{
*head=NULL;
return 0;
}





int DestroyLinkedList( DATANODE *head )
{
DATANODE *tmp_Node = head;
if(head == NULL)/*空链表*/
return 0;
if(head->next == head)/*只有一个节点的链表*/
{
free(head);
return 0;
}
do
{
head = head->next;
free(head->prev);
}while(head->next != tmp_Node);
free(head);
return 0;
}







DATANODE * SearchLinkedList( const DATANODE *head,const KEY *key)
{
DATANODE *tmp_p_Node;
if(head==NULL)/*空链表*/
return NULL;
tmp_p_Node = head->next->prev;
do
{
if(Compare(tmp_p_Node, key)==0){
return tmp_p_Node;
}
tmp_p_Node = tmp_p_Node->next;
}while(head != tmp_p_Node);
return NULL;
}





DATANODE * InsertLinkedList( DATANODE ** head,const DATA_T * data )
{
DATANODE * node;
DATANODE * pRotateNode=NULL;

if(*head==NULL)
{
/*这里处理的是第一次加入链表,这个是根结点*/
*head=MakeNode(data);
(*head)->next = *head;
(*head)->prev = *head;
return *head;
}
/*头结点右侧插入新节点,头结点指针指向新插入的节点*/
node = MakeNode(data);
(*head)->next->prev = node;
node->next = (*head)->next;
node->prev = *head;
(*head)->next = node;
*head = node;
return node;
}







DATANODE * RemoveNode( DATANODE ** head,const KEY * key )
{
DATANODE * curr;
curr=SearchLinkedList(*head,key);
if(curr==NULL)
{
return NULL;
}
curr->next->prev = curr->prev;
curr->prev->next = curr->next;

curr->next = curr->prev = NULL;

return curr;
}






int DeleteNode( DATANODE ** head,const KEY * key )
{
DATANODE * node=RemoveNode( head,key );
if(node==NULL)
{
return -1;
}
free(node);
return 0;
}







int Compare(const DATANODE * pNode,const KEY * pKey)
{
if(pNode == NULL)
return 1;
return CompareKey(&(pNode->data.key),pKey);
}






DATANODE * MakeNode(const DATA_T * data)
{
DATANODE * node;
node=(DATANODE *)malloc(sizeof(DATANODE));
memset(node,0,sizeof(DATANODE));
memcpy(&node->data,data,sizeof(DATA_T));
return node;
}


/******************************************************************************
* Test.c*/
#include "YanLinkedList.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"


int main(int argc, char *argv[])
{
DATANODE *head=NULL;
DATA_T_X TestDatas[100];
int i =0;
InitialLinkedList(&head);


for(i = 0; i < 100; i++)
{
memset(TestDatas[i].key, 0, sizeof(KEY));
sprintf(TestDatas[i].key, "is %d\n", i);

/*InsertLinkedList(&head,&TestDatas[i]);*/
printf(TestDatas[i].key);

}


system("PAUSE");
return 0;
}


...全文
154 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenteanet 2005-05-20
  • 打赏
  • 举报
回复
学习//`
oneonone 2005-05-20
  • 打赏
  • 举报
回复
哦,我明白了,多谢!~
younggle 2005-05-20
  • 打赏
  • 举报
回复
在.cpp中使用.c的时候,在写.c 和对应的.h 时要加上

#if defined(__cplusplus)
extern "C" {
#endif

...............

#if defined(__cplusplus)
}
#endif
oneonone 2005-05-20
  • 打赏
  • 举报
回复
另外 extern关键字的添加是完全没有必要的。
oneonone 2005-05-20
  • 打赏
  • 举报
回复
问题已经解决了.
我注意到( qrlvls(空 气) )在调试我的程序时将 test.cpp改名为 a.c,然后就没有问题了。
所以问题的关键在于我在 test.cpp 中引用了 c 的函数。我也将.cpp改为.c后就没有问题了。

我推断问题的原因在于VC的编译器会在对.cpp函数进行编译时加上 __cdecl 关键字,但YanLinkedList.c是c文件,它可能不会加这个关键字,或者加了其它的关键字。导致 test.cpp寻找加了__cdecl 关键字的函数时没有找到。

以上原因仅仅是我的推断。
huxzjqhh 2005-05-20
  • 打赏
  • 举报
回复
项目-〉添加现有项-〉YanLinkedList.c
qrlvls 2005-05-20
  • 打赏
  • 举报
回复
没有问题
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.c
Linking...

a.exe - 0 error(s), 0 warning(s)
oneonone 2005-05-19
  • 打赏
  • 举报
回复
我加过extern了,还是不行
hard_stone 2005-05-19
  • 打赏
  • 举报
回复
外部函数前要加声明 extern int InitialLinkedList( DATANODE **head );
extern int DestroyLinkedList( DATANODE *head );
zkxz 2005-05-19
  • 打赏
  • 举报
回复
加好之后要Rebuild All
koko1998 2005-05-19
  • 打赏
  • 举报
回复
gz
oneonone 2005-05-19
  • 打赏
  • 举报
回复
YanLinkedList.c 确实已经加入工程了.

加上extern还是不行
younggle 2005-05-19
  • 打赏
  • 举报
回复
把 YanLinkedList.c 也加到工程里面。这样就没有问题了。
zkxz 2005-05-19
  • 打赏
  • 举报
回复
在YanLinkedList.h中将所有test.c中会用到的变量及函数的前面都加上extern
huxzjqhh 2005-05-19
  • 打赏
  • 举报
回复
是不是YanLinkedList.c没有加入工程?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧