-4
//PROGRAM:
#include<iostream>
#include<conio.h>
using namespace std;
class complex {
private:
    double re, img;
public:
    complex();
    void input();
    complex SubCom(complex c1, complex c2);
    complex SumCom(complex c1, complex c2);
    complex MulCom(complex c1, complex c2);
    void show();
};
complex::complex() {
    re = 0;
    img = 0;
}
void complex::input() {
    cout << "Real:";
    cin >> re;
    cout << "Imagnary:";
    cin >> img;
}
complex complex::SumCom(complex c1, complex c2) {
    re = c1.re + c2.re;
    img = c1.img + c2.img;
}
complex complex::SubCom(complex c1, complex c2) {
    re = c1.re - c2.re;
    img = c1.img - c2.img;
}
complex complex::MulCom(complex c1, complex c2)
{
    re = c1.re * c2.re;
    img = c1.img*c2.img;
}
void complex::show() {
    cout << re << "," << img << "i";

}

int main() {
    complex c1;
    c1.input();
    c1.show();
    complex c2;
    c2.input();
    c2.show();
    complex c;
    c.SumCom(c1, c2);
    c.show();
    c.MulCom(c1, c2);
    c.show();
    c.SubCom(c1, c2);
    c.show();
    _getch();
    return 0;
    system("pause");
}

Hello, I have made a program which takes to integers from the user using a function which is a class member. I have made 3 member functions for sum, product and difference. now the error is showing which says that

 1. complex::SumCom must return a value 
 2. complex::MulCom must return a value
 3. complex::SubCom must return a value.
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 1
    `complex();` this is the only constructor I can see in your code all others are member functions which returns `complex` and you have not returned anything. – JeJo Sep 29 '18 at 16:13
  • but i want it to be void..n dont return anything – Noob student Sep 29 '18 at 16:17
  • If you want a (member) function which returns nothing you should make the return type `void`. E.g. `SumCom` is declared and defined with return type `complex`. (The type before the function name is the return type, you know?) – Scheff's Cat Sep 29 '18 at 16:21
  • EDIT: the program is working fine in dev c++, but in visual studio its showing error – Noob student Sep 29 '18 at 16:24
  • @jejo thanks but no thanks since m an engg student – Noob student Sep 29 '18 at 16:26
  • _the program is working fine in dev c++_ By 'working', I assume you mean 'compiling', and it shouldn't be for reasons already given by other commentors. – Paul Sanders Sep 29 '18 at 16:28
  • Formally, writing a function that's supposed to return a value (such as `complex complex::SumCom(complex c1, complex c2)` and not returning a value produces undefined behavior if you call that function. Both compilers are right here; visual studio is being more helpful by telling you that there's a problem. Regardless, fix it: either return a value from those functions or change the return type of each one from `complex` to `void`. – Pete Becker Sep 29 '18 at 16:40
  • thanks alot,,,i was confused cause the dev c++ compiler wasnt showing any error. N also, i did try writing void with the function but the errors just increased(maybe i was doing something wrong). So i was confused as to how should i return it – Noob student Sep 29 '18 at 17:16

2 Answers2

1

Here

complex SubCom(complex c1, complex c2);
^^^^^^^
complex SumCom(complex c1, complex c2);
^^^^^^^
complex MulCom(complex c1, complex c2);
^^^^^^^

you have promised to return a complex and in any of your member functions definitions, you are not returning it. This is undefined behavior and you are lucky that VS gave you a compiler error. Regarding the compiler errors, try to enable compiler warnings to see and rescue yourself having such kind of undefined behavior in your code.

The solution is to add a return statement to your member functions which would look like(for example the SumCom() should be)

complex complex::SumCom(complex c1, complex c2) 
{
     // implementation
     return /*resulting complex object*/;
}

However, I would suggest to overload +, -, * operators as the three member functions look like perfect candidates for that.

Here is an example code:

#include<iostream>

class complex
{
private:
    double re, img;
public:
    complex() = default;

    friend complex operator+(complex c1, const complex& c2) {
        c1.re += c2.re;
        c1.img += c2.img;
        return c1;
    }
    friend complex operator-(complex c1, const complex& c2) {
        c1.re -= c2.re;
        c1.img -= c2.img;
        return c1;
    }
    friend complex operator*(complex c1, const complex& c2) {
        c1.re *= c2.re;
        c1.img *= c2.img;
        return c1;
    }
    void input();
    void show();
};
void complex::input()
{
        std::cout << "Real:" ; std::cin >> re;
        std::cout << "Imaginary:" ; std::cin >> img;
}
void complex::show() {
    std::cout << re << "," << img << "i\n";

}

int main()
{
    complex c1;
    c1.input();
    c1.show();
    complex c2;
    c2.input();
    c2.show();
    complex c = c1 + c2; // now you can
    c.show();
    c = c1 * c2; // now you can
    c.show();
    c = c1 - c2; // now you can
    c.show();
    return 0;
}

stdin:

Real:Imaginary:1,1i
Real:Imaginary:2,2i

Output:

3,3i
2,2i
-1,-1i
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • thanks alot,,,i was confused cause the dev c++ compiler wasnt showing any error. N also, i did try writing void with the function but the errors just increased. So i was confused as to how should i return it. – Noob student Sep 29 '18 at 17:14
  • 1
    A missing `return` is usually a warning if provided at all. – user4581301 Sep 29 '18 at 18:23
  • @SawakoKuronuma To get the better coding experience always enable extra compiler warnings. I am not sure about dev c++, but [GCC already shows](https://wandbox.org/permlink/yadhVgiLszrh8NR1) while compiling, which should direct you to the issue in your code, – JeJo Sep 30 '18 at 00:15
0

You must write return (object); when you want to return some value. And remember that the value you returned will not display on the screen(maybe console?) unless some functions are called and the returned value was passed to it.

L5en
  • 1
  • Headed in the right direction, but when a function promises to return a value but but is allowed to return without returning a value, the results are undefined. Anything can happen, with some results being more likely (providing bad results) than others (opening a portal to Hell). – user4581301 Sep 29 '18 at 18:33