-5

I am going through the book "Beginning C++ Through Game Programming". I have typed this supplied script perfectly (I even pasted over it with the script supplied from online download- and used undo/redo to compare every character- to find no difference- except line endings and actually capitalizing the first character of function names). Despite no change, the supplied script compiles perfectly fine, but mine does not (unless I copy/paste supplied script). I am given the error in the second function: GoodSwap()

#include<iostream>
using namespace std;

void BadSwap(int x, int y);
void GoodSwap(int* const pX, int* const pY);

int main(){
    int myScore = 150;
    int yourScore = 1000;
    cout << "Original Values" << endl;
    cout << "myScore  : " << myScore << endl;
    cout << "yourScore: " << yourScore << endl;

    cout << "Calling BadSwap()" << endl;
    BadSwap(myScore, yourScore);
    cout << "myScore  : " << myScore << endl;
    cout << "yourScore: " << yourScore << endl;

    cout << "Calling GoodSwap()" << endl;
    GoodSwap(&myScore, &yourScore);
    cout << "myScore  : " << myScore << endl;
    cout << "yourScore: " << yourScore << endl;

    return 0;
}

void BadSwap(int x, int y){
    int temp = x;
    x = y;
    y = temp;
}

void GoodSwap(int* const pX, int* const pY){
    int temp = *pX
    *pX = *pY;
    *pY = temp;
}
Jonas
  • 6,915
  • 8
  • 35
  • 53
  • 1
    No, C++ program is not a script. No, `GoodSwap` is very bad. The error message is very literal. –  Jan 20 '17 at 03:15
  • I think `Good` here means *it works*. :-) – zhm Jan 20 '17 at 03:19
  • `int temp = *pX` Anyway, recommend C++ books is listed [here](http://stackoverflow.com/q/388242/4115625) – Danh Jan 20 '17 at 03:48
  • @Danh , I am seriously hitting the wall on this one. I spent 15 minutes looking over every line, and to not notice that- even after copy/paste :/ . And thank's for the book's page. I have been there before, but I ended up opting for this book as it taught using game concepts as a core- which has been fairly easy to learn (I come from using C# with Unity, but wanted to go more "mainstream" with a more sellable language). – Tim Leitzke Jan 20 '17 at 04:41

1 Answers1

-1

You missed a ; at first line of GoodSwap().

int temp = *pX  // There should be a `;`

After this change, your program can compile in VS2015.

zhm
  • 3,513
  • 3
  • 34
  • 55
  • Thank you. I came from Python, after 2 years of using it. These ';' look invisible half the time still. I +1'd the response, but I don't have much reputation. And I used Notepad++ w/ gcc compiler. – Tim Leitzke Jan 22 '17 at 06:50
  • `gcc` can compile this too, use command `gcc -lstdc++ yourCppFileName.cpp`. The better way to compile C++ program is to use `g++`, it will automatically link to C++ runtime library. The `g++` command would be as simply as `g++ yourCppFileName.cpp`. – zhm Jan 22 '17 at 07:03
  • My basic structure so far that I have been using is "g++ -o main main.cpp" – Tim Leitzke Jan 22 '17 at 07:27