请问Thinking C++ 的一个题目

pp_dll 2005-10-17 05:00:09
//: C08:StringStack.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Using static const to create a
// compile-time constant inside a class



#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[] = {
"pralines & cream",
"fudge ripple",
"jamocha almond fudge",
"wild mountain blackberry",
"raspberry sorbet",
"lemon swirl",
"rocky road",
"deep chocolate fudge"
};

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;
} ///:~



请问为什么 static const int size = 100;放在类里通不过,哪个高手能不能给我讲讲Const

...全文
358 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
K 2005-10-18
  • 打赏
  • 举报
回复
新的编译器支持这种写法
gwhnc 2005-10-18
  • 打赏
  • 举报
回复
编译器版本问题,新的编译器应该能够支持
syscoder 2005-10-18
  • 打赏
  • 举报
回复
请问为什么 static const int size = 100;放在类里通不过,哪个高手能不能给我讲讲Const
即使在类体中赋值了,仍须要在类体外进行定义,否则就会出错
但这种写法在vc6下通不过,devc++没有问题的
jpopop 2005-10-18
  • 打赏
  • 举报
回复
class StringStack {
static const int size;
..............

};

const StringStack::size = 100;
JohnTitor 2005-10-18
  • 打赏
  • 举报
回复
————————————————————————————————
图像处理工具软件
通过图像处理工具软件的编制,熟悉并掌握图像处理软件的设计过程。
实验内容:
(1)具有图像大小调整、翻转、投影、平移、缩放、旋转、分割功能;
(2)能够显示直方图;
(3)具有图像增强功能(例如:去噪、灰度、平滑、锐化等);
(4)具有图像压缩功能;
(5)可将图像文件保存、打开、重新编辑。

————————————————

不小了
pp_dll 2005-10-18
  • 打赏
  • 举报
回复
我已经知道了,谢谢各位。
现在我想要做一个小Project,要求如下:

————————————————————————————————
图像处理工具软件
通过图像处理工具软件的编制,熟悉并掌握图像处理软件的设计过程。
实验内容:
(1)具有图像大小调整、翻转、投影、平移、缩放、旋转、分割功能;
(2)能够显示直方图;
(3)具有图像增强功能(例如:去噪、灰度、平滑、锐化等);
(4)具有图像压缩功能;
(5)可将图像文件保存、打开、重新编辑。

————————————————
我希望不用MFC,直接用C++提供的库来完成,我该怎么入手。
pengwjeagles 2005-10-18
  • 打赏
  • 举报
回复
对 类的静态成员 要在外部定义!
oceanblueboy 2005-10-18
  • 打赏
  • 举报
回复
zhaokugua(老小笨笨)说得不错,静态数据成员只能在类声明外分配成员和初始化
zhaokugua 2005-10-18
  • 打赏
  • 举报
回复
静态常量的初始化只能在类的外边进行,虽然新的标准好象对整形的可以放在里面,但至少VC6还不行,
csdolauli 2005-10-18
  • 打赏
  • 举报
回复
要在外面再定义一下const int StringStack::size;
chenminyi 2005-10-18
  • 打赏
  • 举报
回复
static 成员属于类型,所有该类对象共有。在一本数据结构的书上就用这个方法来实现new,可能也与编译器有关,C++ 程序设计语言上有些代码在VC上就不行
Pigwen 2005-10-18
  • 打赏
  • 举报
回复
书上不是说有些编译器可能不支持这种写法得嘛。新的编译器应该没有问题的
kobefly 2005-10-17
  • 打赏
  • 举报
回复
静态变量是属于整个类的

不是某个对象的

qhfu 2005-10-17
  • 打赏
  • 举报
回复
请问为什么 static const int size = 100;放在类里通不过,哪个高手能不能给我讲讲Const

在新的编译器中应该是可以的, 这是c++标准中规定的,,

bm1408 2005-10-17
  • 打赏
  • 举报
回复
基础知识,你应该看看static类型类变量到底在那里开辟,
pp_dll 2005-10-17
  • 打赏
  • 举报
回复
也就是摘自Thinking C++的那段代码编译通不过,是不是作者错了???

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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