在 C++ 中使用构造函数进行类型转换不起作用?

type transform by using constructor in c++ didn't work?

本文关键字:类型转换 不起作用 构造函数 C++      更新时间:2023-10-16

我的代码无法正常运行,谁能告诉我我的otest发生了什么.cpp?

: ~$ g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//filename: otest.cpp
#include <iostream>
using namespace std;
class Complex;   // declaration
class Victor
{
    private:
            int x, y;
    public:
            Victor(int xx = 0, int yy = 0) { x = xx;  y = yy; }
            friend ostream & operator <<(ostream & ostr, Victor & v); 
//              operator Complex() { return Complex(x,y); }
            int getX() { return x;}
            int getY() { return y;}
            Victor(Complex & c) { x = c.getReal(); y = c.getImg(); }
};
class Complex
{
    private:
            int real, img;
    public:
            Complex(int re= 0, int im = 0) { real = re; img = im; }
            friend ostream & operator<<(ostream & ostr,Complex & c);
  //              operator Victor() { return Victor(real,img); }
            int getReal() { return  real;}
            int getImg() { return img; }
            Complex(Victor & v) { real = v.getX(); img = v.getY(); }
};
std::ostream& operator<< (std::ostream& ostr, Complex& c)
{       ostr << c.real << " + " <<c.img << " i";
    return ostr;
}
std::ostream& operator<< (std::ostream& ostr, Victor v)
{
    ostr << "( " << v.x << ", " << v.y << " )" ;
    return ostr;
}
int main()
{
    Victor v1(3,5);
    Victor v2;
    Complex c1(5,6);
    Complex c2;
    v2 = Victor(c1);
    cout << v2 << endl;
    cout << c1<<endl;
    c2 = Complex(v1);
    cout << c2 <<endl;
    cout << v1 <<endl;
    return 0;
}

按顺序处理错误,并依次修复每个问题。

第一个错误:

test.cpp: In constructor ‘Victor::Victor(Complex&)’:
test.cpp:17:40: error: invalid use of incomplete type ‘class Complex’
             Victor(Complex & c) { x = c.getReal(); y = c.getImg(); }

不能在此处使用 Complex 类型,因为它尚未定义。只需在此时声明函数:

Victor(const Complex &);

并在定义Complex之后定义它

Victor::Victor(const Complex & c) { x = c.getReal(); y = c.getImg(); }

(我冒昧地添加了const因为这在这里很合适。您可能还想重命名类Vector,因为它似乎表示向量而不是胜利者。

第二个错误:

test.cpp:10:17: error: ‘int Victor::x’ is private
test.cpp:43:23: error: within this context
    ostr << "( " << v.x << ", " << v.y << " )" ;

这是因为您声明了一个具有一个签名的函数(通过引用获取其第二个参数(

friend ostream & operator <<(ostream & ostr, Victor & v);

并定义一个不同的重载,按值获取其参数:

std::ostream& operator<< (std::ostream& ostr, Victor v)

使两者匹配; const Victor&是最合适的类型。或者,您可以在类定义中定义函数:

class Victor
{
    // ...
    friend ostream & operator <<(ostream & ostr, const Victor & v) {
        return ostr << "( " << v.x << ", " << v.y << " )" ;
    }
    // ...
};

或者,您可以使用公共接口实现该函数,而无需friend声明:

std::ostream& operator<< (std::ostream& ostr, const Victor & v) {
    return ostr << "( " << v.getX() << ", " << v.getY() << " )" ;
}

修复这些错误后,代码将编译并生成看起来合理的输出。

代码不应该被编译,因为在这一点上构造函数

Victor(Complex & c) { x = c.getReal(); y = c.getImg(); }

定义编译器不知道类Complex是否有方法getRealgetImg

您只需在类 Victor 中声明构造函数,并且仅在类 Complex 的定义之后定义它。

考虑到某些成员函数和对象最好具有限定符 const。例如,函数getRealgetImg可以用限定符 const 声明。此外,上面构造函数中的参数也可以用限定符 const 声明。

Victor( cosnt Complex &c );

或者例如operator <<可以声明为

friend std::ostream & operator <<( std::ostream &ostr, const Complex &c );
friend std::ostream & operator <<( std::ostream &ostr, const Victor &v );