| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1400 人关注过本帖
标题:类中常量定义出错,该怎么解决呢
只看楼主 加入收藏
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
 问题点数:0 回复次数:6 
类中常量定义出错,该怎么解决呢

书本上的例程,但是我按照书上的敲上去结果却不对……
//这个是头文件
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char *s);
String();
String(const String &);
~String();
int length()const{return len;}
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
friend bool operator<(const String &st,const String & st2);
friend bool operator>(const String &st,const String & st2);
friend bool operator==(const String &st,const String & st2);
friend ostream & operator<<(ostream & os,const String & st);
friend istream & operator>>(istream & is,const String & st);
static int HowMany();
};
#endif
//这个是类的实现文件
#include <cstring>
#include "string1.h"
using std::cin;
using std::cout;
int String::num_strings=0;
int String::HowMany()
{
return num_strings;
}
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String()
{
len =4;
str=new char[1];
str[0]='\0';
num_strings++;
}
String::String(const String &st)
{
num_strings++;
len=st.len;
str=new char[len+1];
strcpy(str,st.str);
}
String::~String()
{
--num_strings;
delete []str;
}
String & String::operator =(const String & st)
{
if(&st==this)
return *this;
delete []str;
len=st.len;
str=new char[len+1];
strcpy(str,st.str);
return *this;
}
String &String::operator =(const char *s)
{
delete []str;
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}
char & String::operator [](int i)
{
return str[i];
}
const char & String::operator [](int i) const
{
return str[i];
}
bool operator<(const String & st,const String &st2)
{
return(strcmp(st.str,st2.str)<0);
}
bool operator==(const String & st,const String & st2)
{
return(strcmp(st.str,st2.str)==0);
}
bool operator>(const String &st,const String &st2)
{
return st2.str<st.str;
}
ostream & operator<<(ostream & os,const String &st)
{
os<<st.str;
return os;
}
istream & operator>>(istream & is,String & st)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st=temp;
while (is && is.get()!='\n')
{
continue;
}
return is;
}
//测试文件
#include <iostream>
#include "string1.h"
const int ArSize=10;
const int MaxLen=81;
int main()
{
using std::cout;
using std::cin;
using std::endl;
String name;
cout<<"Hi, what's your name?\n";
cin>>name;
cout<<name<<",please enter up to "<< ArSize
<<" short sayings <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for(i=0;i<ArSize;i++)
{
cout<<i+1<<": ";
cin.get(temp,MaxLen);
while (cin&&cin.get()!='\n')
{
continue;
}
if (!cin||temp[0]=='\0')
{
break;
}
else
sayings[i]=temp;
}
int total=i;
cout<<"Here are your sayingsL\n";
for(i=0;i<total;i++)
cout<<sayings[i][0]<<": "<<sayings[i]<<endl;
int shortest=0;
int first = 0;
for (i=1;i<total;i++)
{
if (sayings[i].length()<sayings[shortest].length())
{
shortest=i;
}
if (sayings[i]<sayings[first])
{
first = i;
}
}
cout<<"Shortest saying:\n"<<sayings[shortest]<<endl;
cout<<"First alphabetically:\n"<<sayings[first]<<endl;
cout<<"This program used "<<String::HowMany()
<<" String objects.Bye.\n";
return 0;
}
以下是编译信息
Compiling...
sayings1.cpp
e:\c++ primer范例\cp12\string\string1.h(12) : error C2258: illegal pure syntax, must be '= 0'
e:\c++ primer范例\cp12\string\string1.h(12) : error C2252: 'CINLIM' : pure specifier can only be specified for functions
string1.cpp
e:\c++ primer范例\cp12\string\string1.h(12) : error C2258: illegal pure syntax, must be '= 0'
e:\c++ primer范例\cp12\string\string1.h(12) : error C2252: 'CINLIM' : pure specifier can only be specified for functions
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2039: 'CINLIM' : is not a member of 'String'
e:\c++ primer范例\cp12\string\string1.h(7) : see declaration of 'String'
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2065: 'CINLIM' : undeclared identifier
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2057: expected constant expression
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2466: cannot allocate an array of constant size 0
e:\c++ primer范例\cp12\string\string1.cpp(81) : error C2133: 'temp' : unknown size
e:\c++ primer范例\cp12\string\string1.cpp(82) : error C2039: 'CINLIM' : is not a member of 'String'
e:\c++ primer范例\cp12\string\string1.h(7) : see declaration of 'String'
执行 cl.exe 时出错.

1.exe - 1 error(s), 0 warning(s)
看起来问题都出现再红色字体的地方,怎么会这样呢
我用的是VC6.0,会不会是因为这个问题。

[此贴子已经被作者于2007-9-5 21:09:29编辑过]

搜索更多相关主题的帖子: 常量 定义 
2007-09-05 21:08
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
istream & operator>>(istream & is,String & st)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st=temp;
while (is && is.get()!='\n')
{
continue;
}
return is;
}

改为
istream & operator>>(istream & is,const String & st)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st=temp;
while (is && is.get()!='\n')
{
continue;
}
return is;
}

=×&D o I p R e E n C g T l X&×=
2007-09-06 09:23
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 
哦,谢谢,问题已解决
2007-09-06 14:33
lqxwj1015
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-6-14
收藏
得分:0 
楼主 你在istream & operator>>(istream & is,const String & st)定义const类型,那就是不能改变,
但你给它一直在改变它的值,显然是错误的。我不知道你真解决了没有。

所以把这一句声明和定义里const都去掉,同时把
static const int CINLIM = 80;改成enum{CINLIM = 80};就行啦!

我在VC++6.0编绎通过~~~~

2007-09-10 18:21
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
收藏
得分:0 
我是在函数的实现部分把漏掉的const 加上,同时把static const int CINLIM = 80;改成enum{CINLIM = 80};编译通过
但问题是,为什么《c++primer plus》第五版上是用static const int CINLIM = 80呢
2007-09-10 18:52
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
以下是引用heyyroup在2007-9-10 18:52:22的发言:
我是在函数的实现部分把漏掉的const 加上,同时把static const int CINLIM = 80;改成enum{CINLIM = 80};编译通过
但问题是,为什么《c++primer plus》第五版上是用static const int CINLIM = 80呢

static int num_strings;
static const int CINLIM = 80;


既然前者你都在类外定义,那后者你怎么就省略了?


Fight  to win  or  die...
2007-09-10 20:31
lqxwj1015
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-6-14
收藏
得分:0 
他说得没错!!!!! 因为VC++6.0编绎器可能不支持在类声明中初使化静态常量 。VS.2003可能就支持吧。所以你应该
在类声明中声明static const int CINLIM ;
在类外初使化 const int String::CINLEN = 80; 就跟初使化 int String::num_strings = 0;一样
这样也可以编绎通过
2007-09-11 18:44
快速回复:类中常量定义出错,该怎么解决呢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015928 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved