gpt4 book ai didi

c++ - 从一种类类型到另一种类类型的数据转换

转载 作者:行者123 更新时间:2023-11-30 01:47:33 25 4
gpt4 key购买 nike

我试图将一种类类型数据转换为另一种类类型,因此我在我的 C++ oops 程序中使用了转换运算符方法。

class example2;
class example1
{
int id;
int numbers;
int cost;
public:
example1(int a, int b, int c)
{
id=a;
numbers=b;
cost=c;
}

// getid() added here
// getnumber() added here
// getcost() added here

operator example2()
{
example2 temp;
temp.id=id;
temp.value=numbers*cost;
return temp;
}
};
class example2
{
int id;
int value;
public:
example2(int x,int y){
id=x;
value=y;
}
void display(){
cout<<"id "<<id<<endl;
cout<<"value "<<value<<endl;
}
};

当我使用从 example1 到 example2 的转换时。它显示错误。

int main()
{
example1 s1(100,5,140.0);
example2 s2;
s2=s1;
s2.display();
return 0;
}

它给出了错误,但为什么?。我在example1类中创建了一个运算符重载成员函数,因为example1的对象必须更改为example2的对象。所以我认为这个函数只是从类example1的方法中调用的。应该是正确的

错误是这样的:

错误:返回类型“class example2”不完整并且示例 2 温度;类型不完整

我以某种方式解决了这个问题,我在 example2 类端添加了一个构造函数:

example2(example1 e)
{
id=e.getid(); //these functions already added in my code i didnt mentioned them in here.
value=e.getnumber()*e.getcost();
}

并对example1中的'operator example2()'部分进行注释。现在它正在工作。但是以前的方式不接受。请帮助我纠正我以前做这件事的方式。

最佳答案

其中一个类必须先定义,第二个类在定义后才能完全使用。这意味着您必须稍微分解类定义。

这是一个示例,说明如何根据 OP 的帖子获取尚未定义的类。解释以代码中嵌入的注释的形式出现,以将所有代码保存在一个可剪切和粘贴的 block 中。

#include <iostream>
class example2; // forward declaration to satisfy compiler until example2
// is defined
class example1
{
int id;
int numbers;
int cost;
public:
example1(int a, int b, int c)
{
id = a;
numbers = b;
cost = c;
}

example1(const example2 & e) // the const and reference are just because
// no point copying e, and const ensures no
// side effects to e
{
*this = e; // why duplicate code? Just calling operator=
}

example1& operator=(const example2 & e);
// note the lack of an implementation. This is because at this point
// the compiler only knows example2 exists, but not what it looks
// like. Can't copy what what you haven't seen.

int getid() const //const to allow me to use const references.
{
return id;
}
int getnumber() const
{
return numbers;
}
int getcost() const
{
return cost;
}
void display()
{
std::cout << "Example 1" << std::endl;
std::cout << "id " << id << std::endl;
std::cout << "numbers " << numbers << std::endl;
std::cout << "cost " << cost << std::endl;
}

};

class example2
{
int id;
int value;
public:
example2(int x, int y)
{
id = x;
value = y;
}
example2(const example1 &e)
{
*this = e; // once again just calls the equals operator
}

example2 & operator=(const example1 & e) // OK. This time we know what
// example1 looks like and can
// actually implement the method
{
id = e.getid();
value = e.getnumber() * e.getcost();
return *this;
}

int getid() const
{
return id;
}
int getvalue()const
{
return value;
}
void display()
{
std::cout << "Example 2" << std::endl;
std::cout << "id " << id << std::endl;
std::cout << "value " << value << std::endl;
}
};

// and now for the implementation of example1's equals operator
example1& example1::operator=(const example2 & e)
{
id = e.getid();
numbers = -1; //do real work to get cost and numbers from e.getvalue()
cost = -1;
return *this;
}

int main()
{
example1 s1(100, 5, 140.0);
example2 s2(1, 2);
s2 = s1;
s2.display();
example2 s3(314, 278);
s1 = s3;
s1.display();
return 0;
}

现在重载转换运算符。这是你几乎不想做的事情,因为几乎总是有更明显的方法来实现同样的目标。

例如:

#include<iostream>

class Integer
{
public:
Integer(int val):mVal(val)
{
}
operator int()
{
return mVal;
}
int getVal()
{
return mVal;
}
private:
int mVal;
};

int main()
{
Integer t(42);
int x = (int)t; // We're turning the Integer into an int? Kinda makes sense.
std::cout << x << std::endl;
x = t.getVal(); // hey! we're getting the integer's value! Do I need this comment?
std::cout << x << std::endl;
}

关于c++ - 从一种类类型到另一种类类型的数据转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31493792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com