vc 编译出错的小问题
win2000,vc6.0控制台做了小程序。共三个文件,libcpp.h(定义了一个struct结构)
libcpp.cpp(是struct结构的内部函数实现)
testlibcpp.cpp(是一个主函数)遇到一点小困难,请求帮助。
源代码和出错信息如下
我是新手,高手一看会马上知道答案的
源码:
//libcpp.h
struct stash
{
//properties
int size;
int quantity;
int next;
unsigned char* storage;
//functions
void initialize(int Size);
void cleanup();
int add(void* element);
void * fetch(int index);
int count();
void inflate(int increase);
};
//libcpp.cpp
#include "libcpp.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void stash::initialize(int Size)
{
size = Size;
quantity = 0;
next = 0;
storage = 0;
}
void stash::cleanup()
{
if (storage)
{
puts("freeing storage");
free(storage);
}
}
int stash::add(void * element)
{
if (next >= quantity)
inflate(100);
memcpy(&(storage[next * size]), element, size);
next++;
return(next - 1);
}
void * stash::fetch(int index)
{
if (index >= next || index < 0)
return 0;
return &(storage[size * index]);
}
int stash::count()
{
return next;
}
void stash::inflate(int increase)
{
void * v = realloc(storage, (quantity + increase) * size);
assert(v);
storage = (unsigned char *)v;
quantity += increase;
}
//testlibcpp.cpp
#include "libcpp.h"
#include <assert.h>
#include <stdio.h>
#define BUFSIZE 80
int main()
{
stash intStash, stringStash;
int i;
FILE * file;
char buf[BUFSIZE];
char * cp;
intStash.initialize(sizeof(int));
for (i=0; i<100; i++)
intStash.add(&i);
stringStash.initialize(sizeof(char) * BUFSIZE);
file = fopen("testlibcpp.cpp", "r");
assert(file);
while(fgets(buf, BUFSIZE, file))
stringStash.add(buf);
fclose(file);
for (i = 0; i < intStash.count(); i++)
printf("intStash.fetch(%d) = %d\n", i, *(int *)intStash.fetch(i));
i = 0;
while((cp = (char *)stringStash.fetch(i++)) != 0)
printf("stringStash.fetch(%d) = %s", i - 1, cp);
putchar('\n');
intStash.cleanup();
stringStash.cleanup();
return 0;
}