当前位置:编程学习 > C/C++ >>

C++ 基础 不能在一个类中定义另一个类的成员函数

[cpp]  
<span style="font-size:18px;"></span>  
//============================================================================
// Name        : 11.cpp
// Author      : zhaoming
// Version     :
// Copyright   : copyright to zhaoming
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
 
 
class INTSET;  //不完整声明或前向声明
class REALSET{
float *elems;
int card,maxcard;
public:
REALSET(INTSET &s);
~REALSET(){
delete elems;
}
};
class INTSET{
int *elems,card,maxcard;
//注意:不能在一个类的内部定义另一个类的成员函数,但可以声明
/*
* 课本上是这样写的
* friend double REALSET::REALSET(INTSET &s)//自动成为inline函数
{
//本函数声明为INTSET的友元后,可直接访问INTSET的成员
elems = new float[maxcard = s.maxcard];
card = s.card;
for(int i = 0; i < card; i++){
elems[i] = s.elems[i];
}
}
*/
friend  REALSET::REALSET(INTSET &s);//自动成为inline函数
 
 
public:
INTSET(int maxcard);
~INTSET(){
delete elems;
}
int getcard(){
return card;
}
int getelems(int i){
return elems[i];
}
};
REALSET::REALSET(INTSET &s)
{
//本函数声明为INTSET的友元后,可直接访问INTSET的成员
elems = new float[maxcard = s.maxcard];
card = s.card;
for(int i = 0; i < card; i++){
elems[i] = s.elems[i];
}
}
INTSET::INTSET(int max){
elems = new int[maxcard = max];
card = 0;
}
int main()
{
INTSET iset(20);
REALSET rset(iset);
}
 
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,