数据从一种类型转换为另一种类型

时间:2015-07-18 17:41:54

标签: c++ oop

我试图将一个类类型数据转换为另一个类类型,因此我在c ++的oops程序中使用了cast operator方法。

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&#39;方法调用,我认为。它应该是正确的

错误就像:

错误:返回类型&#39; class example2&#39;是不完整的 example2 temp;类型不完整

不知怎的,我以另一种方式解决了这个问题,我在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();
}

并对&#39;运算符example2()&#39;进行评论部分在example1中。 现在它正在运作。 但以前的方式不接受。请帮我纠正我之前做过这件事的方法。

4 个答案:

答案 0 :(得分:3)

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

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

#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;
}

答案 1 :(得分:1)

class example2;
class example1(){
   operator example2()
   {
       example2 temp;
       //more stuff
   }
};

在代码的这个时间点,编译器必须知道example2的完整定义,因为您正在创建一个名为temp的类的实例。但是,此时您只有example2的转发声明。因此,虽然编译器知道example2是一种类型,因为前向声明它在那个时间点没有关于该类的完整信息,因此它无法生成temp。这会给你看错。

如果您要在此处执行的操作能够从example2类型分配给example1,则应为[{1}}创建一个example2的分配运算符第

example1

然而,这对我来说是一个糟糕的设计,可能有更好的方法来实现你想要的涉及不同设计的东西。例如,一个方法的目的是将class example2{ example2& operator= (example1 const& e1){ this->id = e1.id; this->value = e1.numbers * e1.cost; return *this; } }; 作为参数并修改状态将比这更清楚。就像从example1

中的信息构建新的example2一样

答案 2 :(得分:0)

您必须使用返回类型example2才可以使用

答案 3 :(得分:0)

它工作得很好,如果我缺少什么,请告诉我。.
我认为您所缺少的是:

  1. 在class example1之前定义class example2
    2.通过引用返回来访问example2的私有数据成员
    3.目标类example2中的空构造函数

    <button @click.stop="myFunction">Increase</button>
    
相关问题