不需要输出。为什么没有"gx+gy"输出?

output is not desired. why no output for "gx+gy"?

本文关键字:输出 gx+gy 为什么 不需要      更新时间:2023-10-16

我创建了一个类作为A2dd,我希望通过printf从gx+gy输出,但我不认为这是输出。我希望类的输出显示在控制台中(因为我使用eclispe)。但我只看到"你好世界"。

问题出在哪里?

Main:

#include <iostream>
#include <stdio.h>
#include "A2dd.h"
using namespace std;
int main( )
{
A2dd(5,2);
int getsum();
cout << "hello world" << endl;
return 0;
}

标题:

#ifndef A2DD_H_
#define A2DD_H_
class A2dd {
public:
int gx;
int gy;
A2dd(int x, int y);
int getsum();
};
#endif /* A2DD_H_ */

A2dd:

#include <stdio.h>
#include "A2dd.h"
using namespace std;
A2dd::A2dd(int x, int y)
{
gx = x;
gy = y;
}
int A2dd::getsum()
{
 printf ("%d" , gx + gy);
 return 0;
}

A2dd(5,2);构造一个类型为A2dd的未命名对象并立即销毁它。int getsum();声明但不定义一个名为getsum的函数,该函数不接受参数并返回int。这两者都不是你想做的。相反,试试这个:

A2dd value(5,2);
value.getsum();