Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

c++ - Type name is not allowed, (cannot use overloading)

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

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:

type name Complex is not allowed and too few arguments in function call

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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)
{

}

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.

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);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...