0

i made a class as A2dd and i want an output by printf from gx+gy but i dont see as the output. i want that the output which is from the class be shown in the console (since i use eclispe). but i just see "hello world".

where is the problem?

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

header:

#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;
}
Mason
  • 29
  • 1
  • 7
  • 1
    You probably meant, in `main`, `A2dd object(5,2); object.getsum();` – M.M Dec 09 '15 at 03:32
  • 1
    `getsum` is never called? – user253751 Dec 09 '15 at 03:32
  • `int getsum();` in `main()` is a declaration, not a function call. And it isn't a declaration of `A2dd::getsum()` either. – Jonathan Leffler Dec 09 '15 at 03:33
  • 1
    I think you need to read a good C++ introduction book. [Here is a good list of good C++ books.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – RedX Dec 09 '15 at 03:56
  • `int getsum();` <- This just means that there exists a function called `getsum` that takes no parameters and returns an integer. It has no connection whatsoever to the `A2dd` member function of the same name. – David Schwartz Dec 09 '15 at 13:17

1 Answers1

2

A2dd(5,2); constructs an unnamed object of type A2dd and immediately destroys it. int getsum(); declares but does not define a function named getsum that takes no arguments and returns int. Neither of those is what you want to do. Instead, try this:

A2dd value(5,2);
value.getsum();
Pete Becker
  • 74,985
  • 8
  • 76
  • 165