不允许输入类型名称(不能使用重载) [英] Type name is not allowed, (cannot use overloading)

查看:272
本文介绍了不允许输入类型名称(不能使用重载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Complex
{
public:
    Complex(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
    void getComplex(); //get real and imaginary numbers from keyboard
    void sum(Complex, Complex); //method to add two complex numbers together
    void diff(Complex, Complex); //method to find the difference of two complex numbers
    void prod(Complex, Complex); //method to find the product of two complex numbers
    void square(Complex, Complex); //method to change each complex number to its square
    void printComplex(); //print sum, diff, prod, square and "a+bi" form 

private: 
    float real; //float data member for real number (to be entered in by user)
    float imaginary; //float data member for imaginary number (to be entered in by user)
};

void Complex::getComplex()
{
    cout << "Enter real number: ";
    cin >> real;
    cout << "Enter imaginary number: ";
    cin >> imaginary;
}

void Complex::sum(Complex, Complex)
{
    float sum = 0.0;
    sum = real + imaginary;
}

int main()
{
    Complex c;
    c.getComplex();
    c.sum(Complex, Complex);
    c.diff(Complex, Complex);
    c.prod(Complex, Complex);
    c.square(Complex, Complex);
    c.printComplex();

    return 0;
}

c.sum下出现错误(复杂,复杂); 在主体内部(以及 c.diff c.prod c.square 行)。错误是:

I get an error under c.sum(Complex, Complex); inside the main (along with the c.diff, c.prod, and c.square lines). The error is:

类型名称不允许使用复数
too函数调用中的几个参数

我完全不允许使用重载运算符来完成此任务。我该怎么解决呢?代码已缩写为显示相关部分。再次感谢。

I am not allowed to use overloading operators at all to complete this task. What should I do to resolve this? Code has been abbreviated to show relevant parts. Thanks again.

推荐答案

假设:类的sum,diff等方法不应返回任何方法结果或修改传递给它们的类实例,但仅通过std :: cout显示操作的结果。

Assumption: the sum, diff etc. methods of your class are not supposed to return any result or modify the class instances that are passed to them, but only display the result of the operation via std::cout.

请阅读至少 http://www.cplusplus.com/doc/tutorial/functions/,以便了解如何将值传递给函数(或在这种情况下为方法,请参见 http: //www.cplusplus.com/doc/tutorial/classes/

Please read at least http://www.cplusplus.com/doc/tutorial/functions/ in order to understand how values are passed to functions (or in this case methods, see http://www.cplusplus.com/doc/tutorial/classes/ )

虽然您只能通过编写来声明一个方法参数的类型,则必须在定义中指定标识符:
`

While you can declare a method only by writing the types of the arguments, you have to specify identifiers in the definition: `

void Complex::sum(Complex c1, Complex c2)
{

}

然后您可以访问c1的成员和c2,如 c1.imaginary
一样。传递c1和c2的更有效方法是使用常量引用 const Complex& c1 。这样就不会复制任何对象。

Then you can access the members of c1 and c2, like c1.imaginary A more efficient way to pass c1 and c2 would be to use "const references" const Complex& c1. Then no copy of the objects will be made.

实际上,方法sum,diff,prod可以设置为静态方法,因为它们对在main中调用的Complex对象c没有影响(以下我的假设)。因此应该是

Actually, the methods sum, diff, prod could be made static methods, as they do not have effect on the Complex object c they are called on in the main(following my assumption). So it should rather be

Complex::sum(c1, c2);

如果我的假设是错误的,则 c.sum()实际上将对c产生影响,我不明白为什么您需要为该方法使用两个参数。您可以将c1添加到c中。但是,该方法应随后称为 Add 。或者,您必须将总和作为新对象返回,但是该方法被声明为无效。

If my assumption is wrong, and c.sum() shall actually have an effect on c, I do not understand why you need two arguments for the method. You could just add c1 to c. However, the method should then be called Add. Or you have to return the sum as a new object, but the method was declared void.

这篇关于不允许输入类型名称(不能使用重载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆