编译下面第三个文件时总是出现以下错误,也许是很简单的问题,好象是头文件条件编译有问题,请高手指正。
c:\c++ files\stshtst3.cpp(10) : error C2065: 'stash' : undeclared identifier
c:\c++ files\stshtst3.cpp(10) : error C2146: syntax error : missing ';' before identifier 'intStash'
c:\c++ files\stshtst3.cpp(10) : error C2065: 'intStash' : undeclared identifier
c:\c++ files\stshtst3.cpp(12) : error C2228: left of '.add' must have class/struct/union type
c:\c++ files\stshtst3.cpp(15) : error C2146: syntax error : missing ';' before identifier 'stringStash'
c:\c++ files\stshtst3.cpp(15) : error C2065: 'stringStash' : undeclared identifier
c:\c++ files\stshtst3.cpp(18) : error C2228: left of '.add' must have class/struct/union type
c:\c++ files\stshtst3.cpp(20) : error C2228: left of '.count' must have class/struct/union type
c:\c++ files\stshtst3.cpp(21) : error C2228: left of '.fetch' must have class/struct/union type
c:\c++ files\stshtst3.cpp(22) : error C2228: left of '.count' must have class/struct/union type
c:\c++ files\stshtst3.cpp(23) : error C2228: left of '.fetch' must have class/struct/union type文件:stash3.h#ifdef STASH3_H_
#define STASH3_H_class stash
{
int size;
int quantity;
int next;
unsigned char* storage;
void inflate(int increase);
public:
stash(int Size);
~stash();
int add(void* element);
void* fetch(int index);
int count();
};
#endif
-----------------------------------------------------------------------
文件:stash3.cpp#include "stash3.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>stash::stash(int Size)
{
size=Size;
quantity=0;
storage=0;
next=0;
}stash::~stash()
{
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[index * size]);
}int stash::count()
{
return next;
}int stash::inflate(int increase)
{
void* v=realloc(storage,(quantity+increase)*size);
assert(v);
storage=(unsigned char*)v;
quantity+=increase;
}
-----------------------------------------------------------------------
文件:stshtst3.cpp#include "stash3.h"
#include <stdio.h>
#include <assert.h>
#define BUFSIZE 80
void main()
{ stash intStash(sizeof(int));
for(int j=0;j<100;j++)
intStash.add(&j);
FILE* file=fopen("STASHTST.CPP","r");
assert(file);
stash stringStash(sizeof(char) * BUFSIZE);
char buf[BUFSIZE];
while(fgets(buf,BUFSIZE,file))
stringStash.add(buf);
fclose(file);
for(int k=0;k<intStash.count();k++)
printf("intStash.fetch(%d)=%d\n",k,*(int*)intStash.fetch(k));
for(int i=0;i<stringStash.count();i++)
printf("stringStash.fetch(%d)=%s",i,(char*)stringStash.fetch(i++));
putchar('\n');
}
c:\c++ files\stshtst3.cpp(10) : error C2065: 'stash' : undeclared identifier
c:\c++ files\stshtst3.cpp(10) : error C2146: syntax error : missing ';' before identifier 'intStash'
c:\c++ files\stshtst3.cpp(10) : error C2065: 'intStash' : undeclared identifier
c:\c++ files\stshtst3.cpp(12) : error C2228: left of '.add' must have class/struct/union type
c:\c++ files\stshtst3.cpp(15) : error C2146: syntax error : missing ';' before identifier 'stringStash'
c:\c++ files\stshtst3.cpp(15) : error C2065: 'stringStash' : undeclared identifier
c:\c++ files\stshtst3.cpp(18) : error C2228: left of '.add' must have class/struct/union type
c:\c++ files\stshtst3.cpp(20) : error C2228: left of '.count' must have class/struct/union type
c:\c++ files\stshtst3.cpp(21) : error C2228: left of '.fetch' must have class/struct/union type
c:\c++ files\stshtst3.cpp(22) : error C2228: left of '.count' must have class/struct/union type
c:\c++ files\stshtst3.cpp(23) : error C2228: left of '.fetch' must have class/struct/union type文件:stash3.h#ifdef STASH3_H_
#define STASH3_H_class stash
{
int size;
int quantity;
int next;
unsigned char* storage;
void inflate(int increase);
public:
stash(int Size);
~stash();
int add(void* element);
void* fetch(int index);
int count();
};
#endif
-----------------------------------------------------------------------
文件:stash3.cpp#include "stash3.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>stash::stash(int Size)
{
size=Size;
quantity=0;
storage=0;
next=0;
}stash::~stash()
{
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[index * size]);
}int stash::count()
{
return next;
}int stash::inflate(int increase)
{
void* v=realloc(storage,(quantity+increase)*size);
assert(v);
storage=(unsigned char*)v;
quantity+=increase;
}
-----------------------------------------------------------------------
文件:stshtst3.cpp#include "stash3.h"
#include <stdio.h>
#include <assert.h>
#define BUFSIZE 80
void main()
{ stash intStash(sizeof(int));
for(int j=0;j<100;j++)
intStash.add(&j);
FILE* file=fopen("STASHTST.CPP","r");
assert(file);
stash stringStash(sizeof(char) * BUFSIZE);
char buf[BUFSIZE];
while(fgets(buf,BUFSIZE,file))
stringStash.add(buf);
fclose(file);
for(int k=0;k<intStash.count();k++)
printf("intStash.fetch(%d)=%d\n",k,*(int*)intStash.fetch(k));
for(int i=0;i<stringStash.count();i++)
printf("stringStash.fetch(%d)=%s",i,(char*)stringStash.fetch(i++));
putchar('\n');
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货