PDA

View Full Version : C++ class int problem



Shuttleu
05-29-2009, 04:45 AM
I have just learnt classes in C++ and whenever i compile and run the following code i get this :confused:

thomas@Thomas-laptop:~$ cd Documents/C++
thomas@Thomas-laptop:~/Documents/C++$ g++ first.cpp
thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = -1081553448 y = 134514585
thomas@Thomas-laptop:~/Documents/C++$


#include <iostream>
using namespace std;

class TPoint
{
public: int x, y;
};

int main()
{
TPoint tp;
cout << "x = " << tp.x << " y = " << tp.y << '\n';
return 0;
}

where have i gone wrong :confused:

~shut

JAD
05-29-2009, 05:14 AM
You haven't gone wrong anywhere. It compiles and runs perfectly fine for me in Dev C++. You're using g++ though, so idk if that's the problem.

You forgot to set x and y's values, so maybe that's causing a problem too.

Shuttleu
05-29-2009, 05:22 AM
ok i will try setting the vars
but i thought that it would have automatically been set to 0

~shut

EDIT: nope no difference

thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1076505944
thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1081172440
thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1075995880
thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1076958664
thomas@Thomas-laptop:~/Documents/C++$

the y seems to change everytime i run it as well :/

~shut

JAD
05-29-2009, 06:54 AM
It's your compiler. When I run it I consistently get the output:

x = 47 y = 2

Variables are not automatically set to 0 in C++.

Try Dev C++ :)

Shuttleu
05-29-2009, 06:56 AM
is Dev C++ for linux?

~shut

JAD
05-29-2009, 07:04 AM
is Dev C++ for linux?

~shut

I thought it was, but I just checked and it's not. Just search around for a Linux compiler. I really don't know anything at all about linux, so I can't help you there :p

Good luck.

Shuttleu
05-29-2009, 07:09 AM
could you send me the file that you get so i can test it on mine and then i can see wither it is linux or the compiler

~shut

Wizzup?
05-29-2009, 07:49 AM
ok i will try setting the vars
but i thought that it would have automatically been set to 0

~shut

EDIT: nope no difference

thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1076505944
thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1081172440
thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1075995880
thomas@Thomas-laptop:~/Documents/C++$ ./a.out
x = 134520820 y = -1076958664
thomas@Thomas-laptop:~/Documents/C++$

the y seems to change everytime i run it as well :/

~shut

You must always initialise variables. They are assigned in memory that may contain any random value. And, you might want to try Netbeans with C++ Plugin.

Your class doesn't have a constructor.

Shuttleu
05-29-2009, 07:53 AM
You must always initialise variables. They are assigned in memory that may contain any random value. And, you might want to try Netbeans with C++ Plugin.

Your class doesn't have a constructor.

thank you
i changed it to


TPoint tp;
tp.x = 3;
tp.y = 2;
and now i get
x = 3 y = 2

thank you
rep++

~shut

JAD
05-29-2009, 10:38 AM
ok i will try setting the vars
but i thought that it would have automatically been set to 0

~shut

EDIT: nope no difference


thank you
i changed it to


TPoint tp;
tp.x = 3;
tp.y = 2;
and now i get
x = 3 y = 2

thank you
rep++

~shut

I thought you already tried that, lol :p

Wizzup?
05-29-2009, 06:56 PM
thank you
i changed it to


TPoint tp;
tp.x = 3;
tp.y = 2;
and now i get
x = 3 y = 2

thank you
rep++

~shut

Read this about actual constructors:
http://www.fredosaurus.com/notes-cpp/oop-condestructors/constructors.html

That way you can do TPoint tp = new TPoint();

HyperSecret
05-29-2009, 10:04 PM
You must always initialise variables. They are assigned in memory that may contain any random value. And, you might want to try Netbeans with C++ Plugin.

Your class doesn't have a constructor.

That random value is referred to as 'junk' at least in the programming classes that I took.


Read this about actual constructors:
http://www.fredosaurus.com/notes-cpp/oop-condestructors/constructors.html

That way you can do TPoint tp = new TPoint();

Doing is this way though is also different than doing the way you have it done already, as in the memory that is used for that TPoint is then allocated in different places. If you use the 'new' operator it gets created on what is knows as the heap (which means it can/needs to be deleted after use or it is dangling) so whenever you use the 'new' operator to create memory you need to call a 'delete' operator. To the way you are doing it originally you are creating it on what is known as the 'stack' and after it is called to and taken out of scope it falls off the stack. But for it being in the main, main is in scope the whole runtime so it shouldn't ever pop off the stack.

But as wizzup said, in C++ your variables when created get some random number known as junk so if you want them to be 0 it needs to be initialized.

Bobarkinator
05-29-2009, 10:44 PM
You haven't gone wrong anywhere. It compiles and runs perfectly fine for me in Dev C++. You're using g++ though, so idk if that's the problem.

You forgot to set x and y's values, so maybe that's causing a problem too.

Dev c++ also uses g++ last time I checked

Wizzup?
05-30-2009, 10:25 PM
That random value is referred to as 'junk' at least in the programming classes that I took.

I didn't really feel the need for that term, since it doesn't really explain what happens. I might have expressed myself in an unlucky way, since it ofcourse isn't really *random*, but all I meant is that you can't be sure it contains any certain value, really.


Doing is this way though is also different than doing the way you have it done already, as in the memory that is used for that TPoint is then allocated in different places. If you use the 'new' operator it gets created on what is knows as the heap (which means it can/needs to be deleted after use or it is dangling) so whenever you use the 'new' operator to create memory you need to call a 'delete' operator. To the way you are doing it originally you are creating it on what is known as the 'stack' and after it is called to and taken out of scope it falls off the stack. But for it being in the main, main is in scope the whole runtime so it shouldn't ever pop off the stack.

But as wizzup said, in C++ your variables when created get some random number known as junk so if you want them to be 0 it needs to be initialized.

I'm a C guy (for now...), so I don't use new and such generally. ;)
IIRC, calloc() allocates and sets the values to zero. (But you shouldn't use it for just 2 integers...)

Shuttleu
05-31-2009, 12:19 AM
well i havent learnt about pointers yet
im following this tut and i have soo far read chaper 7
http://www.softlookup.com/tutorial/c++/index.asp

~shut

JAD
05-31-2009, 02:12 AM
well i havent learnt about pointers yet
im following this tut and i have soo far read chaper 7
http://www.softlookup.com/tutorial/c++/index.asp

~shut

Some people have problems understanding pointers at the very beginning. I've taken a few classes at community college on C++ programming, so I could help you over msn/irc/whatever if you need it. I normally am not signed into msn, so just pm me if you need any help.

~JAD

Shuttleu
05-31-2009, 03:37 AM
Some people have problems understanding pointers at the very beginning. I've taken a few classes at community college on C++ programming, so I could help you over msn/irc/whatever if you need it. I normally am not signed into msn, so just pm me if you need any help.

~JAD

thanks
i wil see how it goes and will let you know if i need help
have a look here and let me know what you think :)
http://villavu.com/forum/showthread.php?t=45930

~shut