2

I'm a beginner at programming, I checked other questions like mine but I didn't understand anything.

Sadly, I have a code and in that code I created a class. With the help of methods in the class, I'm changing some variables that i created in the class. But when i try to print them out, i can't. Maybe I'm doing something wrong but anyway i need help, My code doesn't show any kind of syntax errors, but when i run the code with F5, console just shuts down and i can't see the updated variables. Instead my debug window has lots of missing file stuff.

( I tried return player.x; kind of things too, same thing happens)

#include <iostream>

using namespace std;

class Entity  {

public:
    int x, y, speed;

    auto Move(int a, int b , int speed)
    {

        x += a * speed;
        y += b * speed;

    }
}; 

int main() 
{
    Entity player;

    player.Move(1, -1, 4);

    cout << player.x << endl;
    cout << player.y << endl;
}

this is the error window

Adding getchar(); or Sleep(5000); or system("pause") type of things can keep my console open, but there is another problem now, i played with low numbers but it printed out something around negative 800 thousand. Thanks for the replies, and i didnt get the undefined behaviour thing. Are you saying that i can't access a variable in main that i created in class "entity"?

Problems are all sorted out thanks to all of you. You shouldn't leave any variables undefined if you are going to play with them, i added some values to them and Now i get 4 and -4 as x and y coordinates, not something around 800 thousand or million stuff. (x and y 0, and speed 1 if any newbies like me is interested).

Reverse54
  • 21
  • 4
  • 1
    Try Ctrl+F5 (assuming Visual Studio) otherwise run with F5 but add `return 0;` at the end of main and put a breakpoint on it. – Borgleader Aug 28 '18 at 12:27
  • 3
    The `x`, `y` and `speed` members of `Entity` are never initialized to any value, so any access to them is *undefined behavior* – UnholySheep Aug 28 '18 at 12:29
  • @Borgleader The Ctrl+F5 option seems to be removed in VS 2017. – Ron Aug 28 '18 at 12:29
  • 1
    please post error messages directly in your question as *text*, not a screenshot – Andriy Tylychko Aug 28 '18 at 12:29
  • your error window is from the msvc debug output. std::cout is going to be seen in the new console window created by the debug session. but if your program just prints and exits it's likely you won't be able to see it before it exits. – Abigail Aug 28 '18 at 12:30
  • @UnholySheep actually member speed is never used in that snippet, it should be shadowed by argument speed, but yes its UB. – Borgleader Aug 28 '18 at 12:30
  • @Ron: no it wasn't – Andriy Tylychko Aug 28 '18 at 12:30
  • 1
    @Ron Note, that the actual shortcut, depending on your configuration settings, might not be `Ctrl+F5`. – Algirdas Preidžius Aug 28 '18 at 12:33
  • BTW, The output window shows nothing out of the ordinary. The can not find pdb files is not a real problem. You only need these if you want to dig deep into the OS when debugging ( maybe you think you have found a bug in your OS or want to understand the interaction between your program and a OS API). You can use a symbol server to download the symbols to remove this warning if you want. – drescherjm Aug 28 '18 at 12:35
  • P.S: `auto Move(int a, int b , int speed)` considering this function doesn't actually return anything, I would strongly suggest changing the return type to `void`. – Borgleader Aug 28 '18 at 12:36
  • Your error messages show a corrupted/bad compiler installation. Nobody here will be able to help you. You need to figure out exactly what's the problem with your compiler or library installation. Perhaps try reinstalling your compiler, and development tools (and all of this is in addition to the numerous, fundamental bugs in the shown C++ code that other people have already commented on). – Sam Varshavchik Aug 28 '18 at 12:37
  • *"i didnt get the undefined behaviour thing"* - how can you be so sure? *Undefined behavior* is, as the name implies, completely **undefined**. Wrong/unlikely numbers like *"around negative 800 thousand"* are very much a possibly outcome of *undefined behavior* – UnholySheep Aug 28 '18 at 13:04
  • In the Entity class, i thought i defined them as an integer with giving them a type, do i also need to give them a value? – Reverse54 Aug 28 '18 at 13:06
  • @UnholySheep "I didnt get" was probably meant as "I didnt understand" not as "it didnt happen for me" – Borgleader Aug 28 '18 at 13:08
  • Yes, that fixed it. Thanks everyone, now i get 4 and -4 as x and y coordinates, not something around 800 thousand or million stuff. (x and y 0, and speed 1 if any newbies like me is interested) – Reverse54 Aug 28 '18 at 13:09
  • You need to initialize `x`, `y` and `speed` before using them. The compiler will not set them to `0` for you. – drescherjm Aug 28 '18 at 13:10

2 Answers2

3

There are no important errors in your "error window" (actually the debug output). The messages about missing PDB files are unimportant, and can be safely ignored, and then you get an exit with code 0 message that tells you that your program ran okay.

Your problem is that the console window is disappearing before you have a chance to see your output, see this question for some approaches that will allow you to see the output before the console window disappears. Most likely, when you've done this, you'll see your program is operating correctly.

Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
1

The problem with your second part is that you are not initializing the x and y variables. using x += e y += you are adding something to a variable that is not initialized.

Try to modify your Entity to this:

class Entity  
{

public:
   Entity():x(0),y(0){}; //this line is the costructor and initializes the x e y variables.
   int x, y;

   auto Move(int a, int b , int speed)
   {
     x += a * speed;
     y += b * speed;
   }
}; 
Federico
  • 743
  • 9
  • 22