读书人

关于static const小弟我真的是不知道错

发布时间: 2012-04-06 12:22:24 作者: rapoo

关于static const我真的是不知道哪里错了~
#include <string>
#include <iostream>
using namespace std;

class StringStack
{
static const int size=100;
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};

StringStack::StringStack():index(0){
memset(stack,0,size*sizeof(string*));
}

void StringStack::push(const string* s)
{
if(index <size)
stack[index++]=s;
}

const string* StringStack::pop(){
if(index <0){
const string* rv=stack[--index];
stack[index]=0;
return rv;
}
return 0;
}

string iceCream[]={
"hello ",
"shit ",
"ok "
};

const int iCsz=sizeof iceCream/sizeof*iceCream;

int main()
{
StringStack ss;
for(int i=0;i <iCsz;i++)
ss.push(&iceCream[i]);
const string* cp;
while((cp=ss.pop())!=0)
cout < <*cp < <endl;
}
E:\CAndCpp\book\StringStack.cpp(7) : error C2258: illegal pure syntax, must be '= 0 '
E:\CAndCpp\book\StringStack.cpp(7) : error C2252: 'size ' : pure specifier can only be specified for functions
E:\CAndCpp\book\StringStack.cpp(8) : error C2065: 'size ' : undeclared identifier
E:\CAndCpp\book\StringStack.cpp(8) : error C2057: expected constant expression
E:\CAndCpp\book\StringStack.cpp(8) : warning C4200: nonstandard extension used : zero-sized array in struct/union
E:\CAndCpp\book\StringStack.cpp(9) : error C2229: class 'StringStack ' has an illegal zero-sized array
E:\CAndCpp\book\StringStack.cpp(51) : warning C4508: 'main ' : function should return a value; 'void ' return type assumed
Error executing cl.exe.

StringStack.exe - 5 error(s), 2 warning(s)

哪位大哥帮我纠错啊,谢谢了!

[解决办法]
vc6吧。要么换编译器,要么用enum代替static const
[解决办法]
static const int size=100;
==========================
这个只声明初始值,未分配的内存
需在类外定义const int StringStack::size;以分配内存的
[解决办法]
vs2005下是这个错误 cout < <*cp < <endl;

error C2679: binary ' < < ' : no operator found which takes a right-hand operand of type 'const std::string ' (or there is no acceptable conversion)
[解决办法]
#include <string>
#include <iostream>
using namespace std;

static const int size=100;
class StringStack
{
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};

StringStack::StringStack():index(0){


memset(stack,0,size*sizeof(string*));
}

void StringStack::push(const string* s)
{
if(index <size)
stack[index++]=s;
}

const string* StringStack::pop(){
if(index> 0){
const string* rv=stack[--index];
stack[index]=0;
return rv;
}
return 0;
}

string iceCream[]={
"hello ",
"shit ",
"ok "
};

const int iCsz=sizeof iceCream/sizeof*iceCream;

int main()
{
StringStack ss;
for(int i=0;i <iCsz;i++)
ss.push(&iceCream[i]);
const string* cp;
while((cp=ss.pop())!=0)
cout < <*cp < <endl;
}


[解决办法]
todototry说的没错,静态成员变量,必须这样使用

读书人网 >C++

热点推荐