如何解决 class 成员函数中的 "return a value" 错误?
How to solve "return a value" Error in a class member function?
//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");
}
你好,我制作了一个程序,它使用一个 class 成员的函数从用户那里获取整数。我为sum、product和difference做了3个成员函数。现在显示的错误是
1. complex::SumCom must return a value
2. complex::MulCom must return a value
3. complex::SubCom must return a value.
这里
complex SubCom(complex c1, complex c2);
^^^^^^^
complex SumCom(complex c1, complex c2);
^^^^^^^
complex MulCom(complex c1, complex c2);
^^^^^^^
你已经承诺到return一个complex
并且在你的任何成员函数定义中,你都没有returning它。这是 undefined behavior 你很幸运 VS 给了你一个编译器错误。关于编译器错误,请尝试启用编译器警告以查看并挽救代码中存在此类未定义行为的情况。
解决方案是向您的成员函数添加一个 return 语句,它看起来像(例如 SumCom()
应该是)
complex complex::SumCom(complex c1, complex c2)
{
// implementation
return /*resulting complex object*/;
}
但是,我建议重载 +
, -
, *
operators
,因为这三个成员函数看起来非常适合。
这是一个 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;
}
标准输入:
Real:Imaginary:1,1i
Real:Imaginary:2,2i
输出:
3,3i
2,2i
-1,-1i
你必须写
return (object);
当你想要 return 一些值时。请记住,除非调用某些函数并将 returned 值传递给它,否则您 returned 的值不会显示在屏幕上(也许是控制台?)。
//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");
}
你好,我制作了一个程序,它使用一个 class 成员的函数从用户那里获取整数。我为sum、product和difference做了3个成员函数。现在显示的错误是
1. complex::SumCom must return a value
2. complex::MulCom must return a value
3. complex::SubCom must return a value.
这里
complex SubCom(complex c1, complex c2);
^^^^^^^
complex SumCom(complex c1, complex c2);
^^^^^^^
complex MulCom(complex c1, complex c2);
^^^^^^^
你已经承诺到return一个complex
并且在你的任何成员函数定义中,你都没有returning它。这是 undefined behavior 你很幸运 VS 给了你一个编译器错误。关于编译器错误,请尝试启用编译器警告以查看并挽救代码中存在此类未定义行为的情况。
解决方案是向您的成员函数添加一个 return 语句,它看起来像(例如 SumCom()
应该是)
complex complex::SumCom(complex c1, complex c2)
{
// implementation
return /*resulting complex object*/;
}
但是,我建议重载 +
, -
, *
operators
,因为这三个成员函数看起来非常适合。
这是一个 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;
}
标准输入:
Real:Imaginary:1,1i
Real:Imaginary:2,2i
输出:
3,3i
2,2i
-1,-1i
你必须写
return (object);
当你想要 return 一些值时。请记住,除非调用某些函数并将 returned 值传递给它,否则您 returned 的值不会显示在屏幕上(也许是控制台?)。