C++ C++;自定义对象的数据转换
在这个程序中,我们使用从a类到c类的数据转换C++ C++;自定义对象的数据转换,c++,C++,在这个程序中,我们使用从a类到c类的数据转换 a(c c1) { return (c1.getd()*100) } // constructor in class a, this is correct, but when when we use c(a a1) { return (a1.getb()/100) } // constructor, then compile error comming that getb is not a member of a please clea
a(c c1)
{
return (c1.getd()*100)
}
// constructor in class a, this is correct, but when when we use
c(a a1)
{
return (a1.getb()/100)
}
// constructor, then compile error comming that getb is not a member of a please clear what is the problem.
#include<iostream.h>
#include<conio.h>
class a;
class c {
int d;
public:
c() {
d=0;
}
c(int x) {
d=x;
}
int getd() {
return d;
}
void putdata() {
cout<<d;
}
c(a a1){
d=(a1.getb()/100);//here is compile error coming --getb is not a member of a
}
};
class a {
int b;
public:
a() {
b=0;
}
a(int x) {
b=x;
}
void putdata() {
cout<<b;
}
int getb() {
return b;
}
};
void main() {
c c1;
a a1=100;
a a2(100);
c1=a1;
c1.putdata();
getch();
}
a(c1)
{
返回(c1.getd()*100)
}
//类中的构造函数,这是正确的,但是当我们使用
c(a a1)
{
返回(a1.getb()/100)
}
//构造函数,然后编译错误,即getb不是a的成员,请澄清问题所在。
#包括
#包括
甲级;
c类{
int d;
公众:
c(){
d=0;
}
c(整数x){
d=x;
}
int getd(){
返回d;
}
void putdata(){
cout声明了一个
,但在遇到该函数定义时未定义。请将定义移到a类
定义之后:
class a;
class c {
int d;
public:
c() { d=0; }
c(int x) { d=x; }
int getd() { return d; }
void putdata() { cout<<d; }
c(a& a1); // Pass a1 as reference as class a is not yet defined.
};
class a
{
...
};
c::c(a& a1)
{
d=(a1.getb()/100);
}
a类;
c类{
int d;
公众:
c(){d=0;}
c(int x){d=x;}
int getd(){return d;}
当您写入时,void putdata(){cout
class a;
您承诺将在稍后阶段定义“a类”
然后,在定义类a之前,您已经使用了它。
在定义了类a之后,通过编写使用类a的代码,可以使代码正常工作
#include<iostream>
#include<conio.h>
using namespace std;
class a;
class c
{
int d;
public:
c():d(0) {}
c(int x):d(x) {}
int getd() {return d;}
void putdata() {cout<<d;}
c(a a1); // defined after class a is defined
};
class a
{ int b;
public:
a():b(0) {}
a(int x): b(x) {}
void putdata() {cout<<b;}
int getb() {return b;}
};
c::c(a a1)
{
// uses class a after it is defined
d=(a1.getb()/100);
}
void main()
{
c c1;
a a1=100;
a a2(100);
c1=a1;
c1.putdata();
getch();
}
#包括
#包括
使用名称空间std;
甲级;
c类
{
int d;
公众:
c():d(0){}
c(int x):d(x){}
int getd(){return d;}
void putdata(){cout编译器总是从顶部解析文本文件,因此在使用方法或函数之前,必须定义它
行class a;
告诉编译器有一个名为a的类,但此时编译器不知道任何成员函数
如果将以下代码放在类c的声明之前,那么编译器知道在类a的对象上使用mehodgetb
不是非法的,即使它不知道该方法的作用
class a
{
public:
int getb();
}
我认为问题在于类的声明顺序。请尝试以下操作:
#include<iostream.h>
#include<conio.h>
class a
{
int b;
public:
a()
{
b=0;
}
a(int x)
{
b=x;
}
void putdata()
{
cout<<b;
}
int getb()
{
return b;
}
};
class c
{
int d;
public:
c()
{
d=0;
}
c(int x)
{
d=x;
}
int getd()
{
return d;
}
void putdata()
{
cout<<d;
}
c(a a1)
{
d=(a1.getb()/100);//here is compile error coming --getb is not a member of a
}
};
int main()
{
c c1;
a a1=100;
a a2(100);
c1=a1;
c1.putdata();
getch();
return 0;
}
#包括
#包括
甲级
{
int b;
公众:
()
{
b=0;
}
a(整数x)
{
b=x;
}
void putdata()
{
请重新格式化你的代码和你的问题…阅读起来很困难,我想问题是因为编译器根本不知道类A有getb函数…你能在类C之前定义类A吗?或者是否有声明它bfrhand..?当我们这样做时,构造函数A(C c1){retrun c1.getd()*100在类别a中,即getd不是cDid的成员。您将参数类型从a
更改为a&
?我只是仔细检查了这一点,结果很好。谢谢,我理解我的错误,这一代码消除了我的所有疑问。很高兴能为您提供帮助。如果您消除了疑问,请继续接受答案。