关于头文件的问题。一定给分!

wolf_me 2001-07-23 07:37:50
我是C++的一名初学者。最近看《C++编程思想(Thinking in C++)》(机械工业出版社),里面有几个练习程序,我依原样输入源码,但是编译出错。其中有一个程序是这样的:
//lib.h文件

/*: LIB.H -- Header file:example C library */
/* Array-like entity created at run-time */
typedef struct STASHtag {
int size; /* Size of each space */
int quantity; /* Number of storage spaces */
int next; /* Next empty space */
unsigned char* storage;
} Stash;

void initialize(Sash* S, int Size);
void cleanup(Stash* S);
int add(Stash* S,void* element);
void* fetch(Stash* S,int index);
int count(Stash* S);
void inflate(Stash* S,int incease);
// end of lib.h

//lib.c
/*:LIB.C -- implementation of example C library */
/* Declare structure and functions: */
#include "..\1\lib.h"
/* Error testing macros: */
#include <assert.h>
/* Dynamic memory alloction functions: */
#include<stdlib.h>
#include<string.h> /* memcpy() */
#include<stdio.h>
void initialize(Stash* S,int Size) {
S->size=Size;
S->quantity=0;
S->storage=0;
S->next=0;
}

void cleanup(Stash* S) {
if(S->storage) {
puts("freeing storage");
free(S->storage);
}
}

int add(Stash* S, void* element) {
/* enough space left? */
if(S->next >= S->quantity)
inflate(S,100);
/* Copy element into storage, starting at next empty space: */
memcpy(&(S->storage[S->next * S->size]),element, S->size);
S->next++;
return (S->next - 1); /* Index number */
}

void* fetch(Stash* S,int index) {
if(index >= S->next || index < 0)
return 0; /* Not out of bounds? */
/* Produce pointer to desired element: */
return (&(S->storage[index*S->size]);
}

int count(Stash* S) {
/* Number of elements in stash */
return S->next;
}

void inflate(Stash* S,int increase) {
void* V=realloc(S->storage, (S->quantity + increase) * S->size);
/* Was it successful? */
assert(v);
S->storage = v;
S->quantity += increase;
}

// end of lib.c

// libtestc.c
/*: LIBTESTC.C -- Test demonstration library */
#include "..\1\lib.h"
#include <stdio.h>
#include <assert.h>
#define BUFSIZE 80

main() {
Stash intStash, stringStash;
int i;
FILE* file;
char buf[BUFSIZE];
char* cp;
/* .... */
initialize(&intStash,sizeof(int));
for(i=0;i<100;i++)
add(&intStash,&i);
/* Holds 80-character strings: */
initialize(&stringStash,sizeof(char)*BUFSIZE);
file=fopen("LIBTESTC.C","r");
assert(file);
while(fgets(buf,BUFSIZE,file))
add(&stringStash,buf);
fclose(file);

for(i=0; i <count(&intStash); i++)
printf("fetch(&intStash, %d) = %d\n",i,*(int*)fetch(&intStash,i));
i=0;
while((cp=fetch(&stringStash,i++)!=0)
printf("fetch(&stringStash, %d) = %s", i-1,cp);
putchar('\n');
cleanup(&intStash);
cleanup(&stringStash);
}

// end of libtestc.c

我在编译libtestc.c时,出现编译错误:Stash里面的函数都没定义。我以为原因是因为没有在libtestc.c里包含lib.c文件,于是加进了#include "lib.c",结果出现了重复包含lib.h的错误。(这是我的错误吧?是不是.c文件不可以被包含的?)
后来我把lib.c里的函数定义部分复制到libtestc.c里,结果成功编译、运行。
但是书中很多程序都是这么写的(头文件里声明函数,然后在另一个.c文件里定义函数),应该没有错误。那么我的错误到底中哪里呢?应该怎么样把第三个文件(libtestc.c)与第二个文件(lib.c)联系起来呢?
请各位高手不吝赐教!一定给分!
...全文
182 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wolf_me 2001-07-24
  • 打赏
  • 举报
回复
谢谢各位!问题解决了!
原来是这样的:
在project 的树结构里(就是在工程资源管理器里)用"add node"添加节点(即源文件)添加上lib.c,因为project 名称定为libtestc,所以生成的主文件libtestc.c即我输入的源代码。
运行成功。

开始给分了!
lovec 2001-07-24
  • 打赏
  • 举报
回复
是不是路径有问题,还有这样的简单问题,有时根本就不用建立一个工程,可以用命令行编译器直接编译就行了,还有就是<<Think In C++>>,据我推测可能是在Unix下测试的,这个你在试验的时候注意点.
wolf_me 2001-07-24
  • 打赏
  • 举报
回复
也许是我学的太不系统,关于project 的建立与生成我还没在书上看到过。在这里请各位高手指点一二,小生感激不尽!
wolf_me 2001-07-24
  • 打赏
  • 举报
回复
xiterator(xi):
我按你说的做了,还是不行。stash的成员函数在主函数libtestc.c里未定义。还是和以前一样。
怎么样才能把这三个文件联系起来?

alloscdeveloper(初学者) :
你说的我试过,lib.c与libtestc.c里都include<stdio.h>和<assert.h>,运行时会出错。

victorchen_2000(微力) :
我的问题是怎样使libtestc.c 运行的时候会知道lib.c 里的函数定义?
还有include后面可不可以是.c文件? 
victorchen_2000 2001-07-24
  • 打赏
  • 举报
回复
lib.h要修改成:
//lib.h文件

/*: LIB.H -- Header file:example C library */
/* Array-like entity created at run-time */
#ifndef LIB_H
#define LIB_H
typedef struct STASHtag {
int size; /* Size of each space */
int quantity; /* Number of storage spaces */
int next; /* Next empty space */
unsigned char* storage;
} Stash;

extern void initialize(Sash* S, int Size);
extern void cleanup(Stash* S);
extern int add(Stash* S,void* element);
extern void* fetch(Stash* S,int index);
extern int count(Stash* S);
extern void inflate(Stash* S,int incease);
#endif
// end of lib.h
alloscdeveloper 2001-07-24
  • 打赏
  • 举报
回复

把lib.h和lib.c合并即可。
xiterator 2001-07-24
  • 打赏
  • 举报
回复
是不是cOntw.ASM起动模块所调用的WinMain没有在libtestc项目中定义,因为你只定义的main(),之所以会调用WinMain,可能是你所建立的project类型选项所致,如GUI...等类型的工程,
建议a)重新建立一个控制台类型(Win32 Console Application)的project, 重copy code.
b)不用建立项目,copy code 进行compiler.
wolf_me 2001-07-23
  • 打赏
  • 举报
回复
NowCan:
我用的是Borland C++ 5.0(注意:不是Builder!)。
我建立了一个新的project,名为libtestc。然后新建立lib.h、lib.cpp、libtestc.cpp。
运行时出错:
Error: Unresolved external 'WinMain' reference from module c0ntw.ASM
这个错误我还从未见过。翻译过来应该是:“无法解决来自c0ntw.ASM模块的外部WinMain调用。”对吗?
wyzegg 2001-07-23
  • 打赏
  • 举报
回复
因为函数只可以声明一次,所以在工程的别的C文件里使用函数,应该在文件的开头声明一下用
extern void initialize(Sash* S, int Size);
extern void cleanup(Stash* S);
extern int add(Stash* S,void* element);
extern void* fetch(Stash* S,int index);
extern int count(Stash* S);
extern void inflate(Stash* S,int incease);

NowCan 2001-07-23
  • 打赏
  • 举报
回复
要建立一个project,然后把这些.c文件都加入这个project,至于project的建立方法就要看你的编译器了。
wolf_me 2001-07-23
  • 打赏
  • 举报
回复
咦!? 高手们都哪去了?
Help!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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