| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 826 人关注过本帖
标题:[求助]两个对象间的私有成员变量的拷贝 搞不定了
只看楼主 加入收藏
lear
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-2-23
收藏
 问题点数:0 回复次数:6 
[求助]两个对象间的私有成员变量的拷贝 搞不定了

两个对象间的私有成员变量的拷贝,用的是友元函数的机制。但编译却产生
cpp(19) : error C2027: use of undefined type 'floatset'
G:\vc\3.3.cpp(2) : see declaration of 'floatset'
的错误 哪位大虾给指点指点啊。。。菜鸟小弟实在搞不懂啊
#include<iostream.h>
class floatset;
class Intset
{
private:
int num[3];
public:
Intset ( int x,int y, int z)
{
num[0]=x;
num[1]=y;
num[2]=z;
}
void print( )
{
for(int i=0;i<3;i++)
cout<<num[i];
}
friend void floatset::settofloat(Intset &set);
};

class floatset
{
private:
float num[3];
public:
floatset ( float x,float y, float z)
{
num[0]=x;
num[1]=y;
num[2]=z;
}
void print()
{
for(int j=0;j<3;j++)
cout<<num[j];
}
void settofloat(Intset &set)
{
num[0]=set.num[0];
num[1]=set.num[1];
num[2]=set.num[2];
}
};

void main()
{
floatset t(1.0,2.0,3.0);
t.print();
Intset c(7,8,9);
t.settofloat(c);
t.print();
}

搜索更多相关主题的帖子: 成员 变量 对象 拷贝 
2006-03-20 17:07
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

依据本人的理解,友元可以是一个函数,称为友员函数。友员也可以是一个类,为友员类。
楼主把友元函数定义在类体内是错误的,因为友函数应定义在类体外。按照楼主的意思应该定义一个友员类,而不是友员函数。
还有楼主要拷贝的私有成员变量类型不一致,会有错误产生。
以下是本人的更正,如有不对之处,希望高人指点
#include<iostream.h>
class floatset;
class Intset
{
private:
float num[3];
public:
Intset ( float x,float y, float z)
{
num[0]=x;
num[1]=y;
num[2]=z;
}
void print( )
{
for(int i=0;i<3;i++)
cout<<num[i];
}
friend class floatset;
};

class floatset
{
private:
float num[3];
public:
floatset ( float x,float y, float z)
{
num[0]=x;
num[1]=y;
num[2]=z;
}
void print()
{
for(int j=0;j<3;j++)
cout<<num[j];
}
void settofloat(Intset &set)
{
num[0]=set.num[0];
num[1]=set.num[1];
num[2]=set.num[2];
}
};

void main()
{
floatset t(1.0,2.0,3.0);
t.print();
Intset c(7,8,9);
t.settofloat(c);
t.print();
}


woyaochengshuyidianle 我真的什么也不会
2006-03-20 17:31
lear
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-2-23
收藏
得分:0 

谢谢楼上指点。。。
但是。。能否用友员函数来实现这一功能呢?

2006-03-20 22:11
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

#include <iostream>
#include <cstdlib>
using namespace std;

class Floatset;
class Intset
{
private:
int num[3];
public:
Intset(int x, int y, int z)
{
num[0] = x;
num[1] = y;
num[2] = z;
}
Intset(const Intset & set)
{
num[0] = set.num[0];
num[1] = set.num[1];
num[2] = set.num[2];
}
void print()
{
for(int i = 0; i < 3; i++)
cout<<num[i]<<\" \";
cout<<endl;
}
int * getNumArray(){return num;}
friend void settofloat(Intset & set, Floatset & fset); // 友元函数的声明
};



class Floatset
{
private:
float num[3];
public:
Floatset(int x, int y, int z)
{
num[0] = (float)x;
num[1] = (float)y;
num[2] = (float)z;
}
Floatset (float x, float y, float z)
{
num[0] = x;
num[1] = y;
num[2] = z;
}
void print()
{
for(int j=0;j<3;j++)
cout<<num[j]<<\" \";
cout<<endl;
}
void settofloat(Intset & set)
{
int * p = set.getNumArray();
num[0] = (float)p[0];
num[1] = (float)p[1];
num[2] = (float)p[2];
}
};

void settofloat(Intset & set, Floatset & fset) // 友元函数的定义
{
fset.settofloat(set);
}

int main()
{
Floatset t(1.0f,2.0f,3.0f);
t.print();
Intset c1(7,8,9);
t.settofloat(c1);
t.print();

Intset c2(12, 24, 36);
settofloat(c2, t);
t.print();

system(\"pause\");
return 0;
}



自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-21 16:07
柳儿
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:1830
专家分:30
注 册:2004-9-23
收藏
得分:0 

终于搞明白了。要声明成友元函数,定义必须先出现。这样floatset就要写在前面。因为函数中出现了Intset的引用。所以这个成员函数体的声明要放在Intset之后。这样就没有编译错误了。也是按照楼主的意愿,一个类的成员函数是另一个类的友元函数。
[CODE]
#include<iostream.h>

class Intset;

class floatset
{
private:
float num[3];
public:
floatset ( float x,float y, float z)
{
num[0]=x;
num[1]=y;
num[2]=z;
}
void print()
{
for(int j=0;j<3;j++)
cout<<num[j];
}
void settofloat(Intset &set);

};

class Intset
{
private:
int sum[3];
public:
Intset ( int x,int y, int z)
{
sum[0]=x;
sum[1]=y;
sum[2]=z;
}
void print()
{
for(int i=0;i<3;i++)
cout<<sum[i];
}
friend void floatset::settofloat(Intset &set);
};

void floatset::settofloat(Intset &set)
{
num[0] = set.sum[0];
num[1] = set.sum[1];
num[2] = set.sum[2];
}

void main()
{
floatset t(1.0,2.0,3.0);
t.print();
Intset c(7,8,9);
t.settofloat(c);
t.print();
}

[/CODE]


成功会使人骄傲。如果你骄傲自大,你就会停止学习。不学习,人就停止了进步
2006-03-21 16:16
lear
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-2-23
收藏
得分:0 

两位斑竹可谓尽心尽责。。令人感激涕零啊。。谢过了

2006-03-21 17:20
badstreams
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-11-23
收藏
得分:0 

#include<iostream.h>
//class floatset;
class Intset;
class floatset
{
private:
float num[3];
public:
floatset ( float x,float y, float z)
{
num[0]=x;
num[1]=y;
num[2]=z;
}
void print()
{
for(int j=0;j<3;j++)
cout<<num[j];
}
void settofloat(Intset &set);

};
class Intset
{
private:
int num[3];
public:
Intset ( int x,int y, int z)
{
num[0]=x;
num[1]=y;
num[2]=z;
}
void print( )
{
for(int i=0;i<3;i++)
cout<<num[i];
}
friend void floatset::settofloat(Intset &set);
};

void floatset::settofloat(Intset &set)
{
num[0]=set.num[0];
num[1]=set.num[1];
num[2]=set.num[2];
}

void main()
{
floatset t(1.0,2.0,3.0);
t.print();
Intset c(7,8,9);
t.settofloat(c);
t.print();
}
一个类的成员函数要作为另一个类的友元函数,必须得先定义这个类才可以

2006-03-21 22:19
快速回复:[求助]两个对象间的私有成员变量的拷贝 搞不定了
数据加载中...
 
   



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

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