我做了一个类作为A2dd,我想要一个来自gx + gy的printf输出,但我不认为是输出。我希望来自类的输出显示在控制台中(因为我使用的是eclispe)。但我只是看到"你好世界"。
问题出在哪里?
主:
#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;
}
答案 0 :(得分:2)
A2dd(5,2);
构造一个类型为A2dd
的未命名对象并立即销毁它。 int getsum();
声明但未定义名为getsum
的函数,该函数不接受任何参数并返回int
。这些都不是你想要做的。相反,试试这个:
A2dd value(5,2);
value.getsum();