0

is this well written ? did i do anything wrong ?

#include "stdafx.h"
#include "iostream"
using namespace std;
class rectangleclass
{
    int w,h;
  public:
        void setnum(int x, int y)
        {
        w=x;
        h=y;
    }
    int area() 
        {
            return w*h;
        }
};
int main()
{
    rectangleclass ra;
ra.setnum(7,2);
cout<<ra.area();
return 0;

} im getting two different errors Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup MSVCRTD.lib and Error 2 fatal error LNK1120: 1 unresolved externals

  • I guess you're using Visual Studio. If so, you chose the wrong project type. You should be choosing "Console", not "Win32". – PaulMcKenzie Nov 03 '15 at 18:14
  • As to "well written", what would happen if you called `ra.area()` immediately after you declared `ra`? – PaulMcKenzie Nov 03 '15 at 18:17
  • Specifically [this](http://stackoverflow.com/a/5260237/1413395) and [this](http://stackoverflow.com/a/22160071/1413395) – πάντα ῥεῖ Nov 03 '15 at 18:17
  • thank you i got that fixed, but when the console opened up it lasted a second before closing....any reason ? – Rida Helbawi Nov 03 '15 at 18:18
  • @RidaHelbawi The reason why the console opens and closes is because the program executes exactly what you've written -- it isn't obligated or required to do anything else. Nothing you wrote stops the console from closing. Either place a statement to "pause" the console (whatever that is), or simply run your application in a command window. – PaulMcKenzie Nov 03 '15 at 18:20
  • i removed the return 0; and replaced it with system("pause"); – Rida Helbawi Nov 03 '15 at 18:22
  • if i called ra.area() immediatly after ra i get 687194768. what is this ? – Rida Helbawi Nov 03 '15 at 18:25
  • @RidaHelbawi So I guess your class is not "well written". You failed to initialize your member variables on construction of the `rectangleclass` object. So the code exhibits undefined behavior. – PaulMcKenzie Nov 03 '15 at 18:55
  • its my first try... how do u recommend to improve/fix it ? – Rida Helbawi Nov 03 '15 at 19:45

0 Answers0