关于静态常量的问题?

waxic 2006-03-06 11:05:24
在类定义中定义一个静态常量,在友员函数中不能访问!
头文件
class String
{
private:
.......
static const int CIN=100;
.......
public:
......
friend void function1(String & st)
......
};

类描述文件中

void function1(String & st)
{
//访问CIN
char temp[String::CIN]; //用作用域操作符
......
}

这样对吗?我在vc中编译不能通过
...全文
1213 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
晨星 2006-03-07
  • 打赏
  • 举报
回复
楼主你在类中声明友元函数的时候,写的是:
istream & operator>>(istream & is, const String & st);
而在定义函数体的时候,写的却是:
istream & operator>>(istream & is, String & st)
“const String& st”和“String & st”类型不匹配(实际上可以构成重载),因此后者并非String类的友元。
joziez 2006-03-07
  • 打赏
  • 举报
回复
As a special case, a const static data member of integral type can be initialized within the class body with a constant value.
作为特例,有序型的const静态数据成员可以在类体中用一常量值初始化.
在类中直接定义静态常量成员是C++增加的一项比较新的特性,一些旧的编译器对此不支持。

一点参考.

laofuxing 2006-03-07
  • 打赏
  • 举报
回复
和编译器有关,如果编译器不支持在类中初始化静态变量,就要在外面初始化一下!
waxic 2006-03-07
  • 打赏
  • 举报
回复
怎么没人回复?
htyro 2006-03-07
  • 打赏
  • 举报
回复
受教了,编译器的问题。
waxic 2006-03-07
  • 打赏
  • 举报
回复
已经调好了,谢谢steedhorse(晨星),我怎么把分数给你?
ihcbo 2006-03-07
  • 打赏
  • 举报
回复
我还在学习中,只能看看大家的说话了!
dragonzxh 2006-03-06
  • 打赏
  • 举报
回复
......
晨星 2006-03-06
  • 打赏
  • 举报
回复
其实也不是什么“新”标准了,98年的标准。-_-
晨星 2006-03-06
  • 打赏
  • 举报
回复
To:河马和熊:
这是符合C++新标准的,这也是为什么VC6不行而VC2003/2005可以。
s_hluo 2006-03-06
  • 打赏
  • 举报
回复
class String
{
private:
.......
static const int CIN=100;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~==>能在这里初始化吗?
.......
public:
......
friend void function1(String & st)
......
};
dragonzxh 2006-03-06
  • 打赏
  • 举报
回复
对吗?老大?
类的static变量在外面没定义有空间吗?
不需要在外面const int String1::CIN = 100;?
好像这个是标准问题吧....
反正偶的vc6也有错误.必须把static const int CIN在类外初始化才可以...
晨星 2006-03-06
  • 打赏
  • 举报
回复
怎么在偶的机器上没问题啊?
你include了定义你那个“String”类的头文件了么?
waxic 2006-03-06
  • 打赏
  • 举报
回复
T0:steedhorse(晨星)
奇怪了,我的怎么就编译不成功呢??我把代码帖一下
//string.h
#ifndef STRING_H_
#define STRING_H_
#include<iostream>
using namespace std;

class String
{
private:
char * str;
int len;
static int num_strings; //number of objects
static const int CINLIM = 80; //cin input limit
//enum {CINLIM=90};
public:
String(const char * s);
String();
String(const String &); //copy constructor
~String();
int length() const
{return len;}

//overload operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;

//friends
friend bool operator<(const String &st1,const String &st2);
friend bool operator>(const String &st1,const String &st2);
friend bool operator==(const String &st1,const String &st2);
friend ostream & operator<<(ostream & os,const String & st);
friend istream & operator>>(istream & is,String & st);

//static function
static int HowMany();

};

#endif

////////////////////////////////////////////////
//string.cpp
include <iostream>
#include <cstring>
using namespace std;
#include "string.h"

int String::num_strings=0;
//static const int CINLIM = 80;

//static method
int String::HowMany()
{
return num_strings;
}
/////////////////////////////////
String::String(const char * s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
cout<<num_strings<<": \" "<< str
<<"\" object created\n";
}
/////////////////////////////////////////////////
String::String()
{
len = 4;

str = new char[1];
str[0]='\0';
num_strings++;
cout<<num_strings<<": \" "<< str
<<"\" object created\n";
}
///////////////////////////////////////////////////
String::String(const String & st)
{
num_strings++;
len=st.len;
str=new char[len+1];
strcpy(str,st.str);
}
////////////////////////////////////////////////////////////
String::~String()
{
cout<<"\" "<<str<<"\" object deleted, ";
--num_strings;
cout<<num_strings<<" left\n";
delete [] str;
}
///////////////////////////////////////////////////////////////
//assign a string to a string
String & String::operator=(const String & st)
{
if(this==&st)
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];
}

//read only char access for const string
const char &String::operator[](int i)const
{
return str[i];
}

bool operator<(const String & st1,const String & st2)
{
return(strcmp(st1.str,st2.str)<0);
}

bool operator>(const String & st1,const String &st2)
{
return st1.str<st2.str;
}

bool operator==(const String & st1,const String & st2)
{
return (strcmp(st1.str,st2.str)==0);
}



ostream & operator<<(ostream & os,const String & st)
{
os<<st.str;
return os;
}

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;
}
就是上面这个函数
rabbit729 2006-03-06
  • 打赏
  • 举报
回复
静态数据成员在类声明中声明,在包含类方法的文件中初始化。初始化时使用作用域操作符来指出静态成员所属的类。但如果静态成员是整型或枚举型const,则可以在类声明中初始化。
另外,VC6.0好像不支持静态常量数据成员,建议改为enum {in CIN=100};不过要是这样的话,友元中的声明应该是char temp[CIN];去掉作用域操作符,因为这种方式声明枚举并不会创建类成员。
晨星 2006-03-06
  • 打赏
  • 举报
回复
To:steedhorse(晨星)
我换了vc.net 2003也没通过, 你怎么编译的,给我看看好吗?
//////////////////////////////////////////////
偶也没做啥,就是新建一个空的Win32 Console工程,然后在里面依次新建这两个文件:

// 文件1:test.h
#ifndef TEST_H
#define TEST_H

class A {
private:
static const int SCI = 10;
public:
friend void f();
};

#endif
// 文件2:Main.cpp
#include <iostream>
using namespace std;

#include "Test.h"

void f() {
char tmp[A::SCI];
cout << sizeof(tmp) << endl;
}

int main() {
f();
return 0;
}

然后就顺利编译通过并能正常运行了。
dragonzxh 2006-03-06
  • 打赏
  • 举报
回复
vc6,我这样编
class String
{
private:
.......
static const int CIN;
.......
public:
......
friend void function1(String & st)
......
};
const int String::CIN = 100;

类描述文件中

void function1(String & st)
{
//访问CIN
char temp[String::CIN]; //用作用域操作符
......
}
gr8bug 2006-03-06
  • 打赏
  • 举报
回复
学习。谢谢老师们。
waxic 2006-03-06
  • 打赏
  • 举报
回复
To:steedhorse(晨星)
我换了vc.net 2003也没通过, 你怎么编译的,给我看看好吗?
晨星 2006-03-06
  • 打赏
  • 举报
回复
楼上用得VC6吧?
加载更多回复(1)

64,663

社区成员

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

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