0

I'm working on coding a Pong game and am having trouble creating objects. I had working code, but it did not include a class or any objects, and since it is for an Object-Oriented Programming class, I needed to fix that. However, now I'm getting a bunch of different error messages (it stops displaying after 100). Most of them appear to be dealing with me not creating objects correctly.

I've never worked with objects in C++ (probably should've, but have just been muddling through this class and getting points deducted for this very reason), but since the semester is winding down, I really need to do well on this project.

Update I've managed to get rid of the majority of the errors, but there are still a few remaining that have me stumped. I have edited my code to reflect the changes I've made and updated the error list.

Here is my code:

My header file:

#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <iostream>
//#include <conio.h>
#include <sstream>
#include <math.h>
//#include <gl\gl.h>
//#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "Opengl32.lib")
#define VK_W 0x57
#define VK_S 0x53
using namespace std;

class Pong {
public:
    Pong();
    std::string int2str;
    void drawText(float x, float y, std::string text);
    void drawPaddle(float x, float y, float width, float height);
    void draw();
    void enable2D(int width, int height);
    void keyboard();
    void vec2_norm(float& x, float &y);
    void updateBall();
    void gameOverCheck();
    void update(int value);

    //window size and update rate
    int width;
    int height;
    int interval; // 60 frames per-second

    //scoring
    int p1Score; //Player 1's score
    int p2Score; //Player 2's score
    int winner;

    //the paddles
    int paddleWidth;
    int paddleHeight;
    int paddleSpeed;
    float paddleLeftX;
    float paddleLeftY;
    float paddleRightX;
    float paddleRightY;

    //the ball
    float ballPositionX; 
    float ballPositionY;
    float ballDirectionX;
    float ballDirectionY;
    int ballSize;
    int ballSpeed;
};

My .cpp file:

#include "stdafx.h"
#include "PongGame.h"
#include <string>
#include <Windows.h>
#include <iostream>
//#include <conio.h>
#include <sstream>
#include <math.h>
//#include <gl\gl.h>
//#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "Opengl32.lib")
#define VK_W 0x57
#define VK_S 0x53
using namespace std;

Pong pong;
Pong::Pong() {
    width = 500;
    height = 300;
    interval = 1000/60;
    p1Score = 0;
    p2Score = 0;
    winner = 0;
    paddleWidth = 10;
    paddleHeight = 80;
    paddleSpeed = 3;
    paddleLeftX = 10.0f;
    paddleLeftY = 50.0f;
    paddleRightX = width - paddleWidth - 10;
    paddleRightY = 50;
    ballPositionX = width / 2;
    ballPositionY = height / 2;
    ballDirectionX = -1.0f;
    ballDirectionY = 0.0f;
    ballSize = 8;
    ballSpeed = 3;
}

std::string int2str(int x) { //used to convert an integer to a string
    std::stringstream ss;
    ss << x;

    return ss.str( );
}

void drawText(float x, float y, std::string text) {
    glRasterPos2f(x, y);
    glutBitmapString(GLUT_BITMAP_8_BY_13, (const unsigned char*)text.c_str());
}

void drawPaddle(float x, float y, float width, float height) {
    glBegin(GL_QUADS);
        glVertex2f(x, y);
        glVertex2f(x + width, y);
        glVertex2f(x + width, y + height);
        glVertex2f(x, y + height);
    glEnd();
}

void draw() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    //draws the paddles
    drawPaddle(pong.paddleLeftX, pong.paddleLeftY, pong.paddleWidth, pong.paddleHeight);
    drawPaddle(pong.paddleRightX, pong.paddleRightY, pong.paddleWidth, pong.paddleHeight);

    //draws the ball
    drawPaddle(pong.ballPositionX - pong.ballSize / 2, pong.ballPositionY- pong.ballSize / 2, pong.ballSize, pong.ballSize);

    //draws the score at the top center of the screen
    drawText(pong.width / 2 - 10, pong.height - 15, int2str(pong.p1Score) + ":" + int2str(pong.p2Score));

    glutSwapBuffers();
}

void enable2D(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
}

void keyboard() { //allows teh paddles to be controled from the keyboard
    //moves left paddle (player 1)
    if (GetAsyncKeyState(VK_W))
        pong.paddleLeftY += pong.paddleSpeed; //move paddle up with "W" key

    if (GetAsyncKeyState(VK_S)) 
        pong.paddleLeftY -= pong.paddleSpeed; //move paddle down with "S" key

    //moves right paddle (player 2)
    if (GetAsyncKeyState(VK_UP)) 
        pong.paddleRightY += pong.paddleSpeed; //move paddle up with "up" arrow

    if (GetAsyncKeyState(VK_DOWN)) 
        pong.paddleRightY -= pong.paddleSpeed; //move paddle down with "down" arrow
}

void vec2_norm(float& x, float &y) {
    float length = sqrt((x * x) + (y * y));

    if(length != 0.0f) {
        length = 1.0f / length;
        x *= length;
        y *= length;
    }
}

void updateBall() { //allows teh ball to move
    pong.ballPositionX += pong.ballDirectionX * pong.ballSpeed;
    pong.ballPositionY += pong.ballDirectionY * pong.ballSpeed;

    if(pong.ballPositionX < pong.paddleLeftX + pong.paddleWidth && pong.ballPositionX > pong.paddleLeftX && pong.ballPositionY < pong.paddleLeftY + pong.paddleHeight && pong.ballPositionY > pong.paddleLeftY) { //if ball is hit by player 1's paddle
        float t = ((pong.ballPositionY - pong.paddleLeftY) / pong.paddleHeight) - 0.5f;
        pong.ballDirectionX = fabs(pong.ballDirectionX); 
        pong.ballDirectionY = t;
    }

    if (pong.ballPositionX > pong.paddleRightX && pong.ballPositionX < pong.paddleRightX + pong.paddleWidth && pong.ballPositionY < pong.paddleRightY + pong.paddleHeight && pong.ballPositionY > pong.paddleRightY) { //if ball is hit by player 2's paddle
        float t = ((pong.ballPositionY - pong.paddleRightY) / pong.paddleHeight) - 0.5f;
        pong.ballDirectionX = -fabs(pong.ballDirectionX); 
        pong.ballDirectionY = t;
    }

    if (pong.ballPositionX < 0) { //if ball hits the top wall
        ++pong.p2Score;
        pong.ballPositionX = pong.width / 2;
        pong.ballPositionY = pong.height / 2;
        pong.ballDirectionX = fabs(pong.ballDirectionX); 
        pong.ballDirectionY = 0;
    }

    if (pong.ballPositionX > pong.width) { //if ball hits the right wall
        ++pong.p1Score;
        pong.ballPositionX = pong.width / 2;
        pong.ballPositionY = pong.height / 2;
        pong.ballDirectionX = -fabs(pong.ballDirectionX); 
        pong.ballDirectionY = 0;
    }

    if (pong.ballPositionY > pong.height) { //ball hits top wall
        pong.ballDirectionY = -fabs(pong.ballDirectionY); 
    }

    if (pong.ballPositionY < 0) { //ball hits bottom wall
        pong.ballDirectionY = fabs(pong.ballDirectionY); 
    }

    vec2_norm(pong.ballDirectionX, pong.ballDirectionY);
}

void gameOverCheck() {
    const int maxScore = 10;
    if(pong.p1Score == maxScore) {
        cout << "Player 1 Wins!" << endl;
        pong.winner = 1;
    }
    else if(pong.p2Score == maxScore) {
        cout << "Player 2 Wins!" << endl;
        pong.winner = 2;
    }
}

void update(int value) {
   keyboard();

   if(pong.winner == 0) {
        updateBall();
        glutTimerFunc(pong.interval, update, 0);
        glutPostRedisplay();
        gameOverCheck();
    }
}

My main file:

//PongGameTest.cpp
#include "stdafx.h"
#include "PongGame.h"
#include <string>
#include <Windows.h>
#include <iostream>
//#include <conio.h>
#include <sstream>
#include <math.h>
//#include <gl\gl.h>
//#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "Opengl32.lib")
#define VK_W 0x57
#define VK_S 0x53
using namespace std;

int _tmain(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 200);
    glutCreateWindow("Pong");

    glutDisplayFunc(pong.draw);
    glutTimerFunc(pong.interval, pong.update, 0);

    pong.enable2D(pong..width, pong.height);
    glColor3f(1.0f, 0.0f, 0.0f);

    glutMainLoop();

    return 0;
}

And I'm sure y'all will hate me for this, but here are the errors I'm getting when I compile:

1>  PongGameTest.cpp
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(25): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(25): error C2228: left of '.draw' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2228: left of '.interval' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2228: left of '.update' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2228: left of '.enable2D' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2059: syntax error : '.'
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(29): error C2228: left of '.glColor3f' must have class/struct/union
1>          type is ''unknown-type''

I'm well aware that nobody is going to read all of those messages (I know I sure wouldn't) but could you possibly skim them and see if you might can figure out what I'm doing wrong? Like I said, I'm almost 100% sure that many of them have to do with me not creating the object correctly and using it's members properly.

  • 2
    Could you cut down the code, to the important part? –  Nov 20 '14 at 09:06
  • The last set of messages with ; (semicolon) missing thing are all because you didn't terminate your statements with semicolon in ponggame.cpp file - change them first (easy). Also, you are trying to access your class member functions/variables that are declared private (in your ponggame.cpp file). Keep the member variables private and the access methods public - simple OO design constraints. – ha9u63a7 Nov 20 '14 at 09:06
  • Just wrapping all your code in one class won't help your grades much. You'll need to review the rest of the course until you get the point. – molbdnilo Nov 20 '14 at 09:24
  • Thanks to your suggestions I was able to clear several of the errors, but am still left with a lot of "undeclared identifier" errors. Any ideas? – HiTechRedneck3 Nov 20 '14 at 09:26
  • Yeah, I know @molbdnilo, it's just that this is due tomorrow so am trying to get as much done as I can. We had to turn in a pre-release which I got an 80 on, even though I didn't even use a class, so am hoping that I can get somewhat close to that if I even use a single class. – HiTechRedneck3 Nov 20 '14 at 09:28

1 Answers1

6

1.

Pong Pong;

is illegal: you can't declare an object with a type name as identifier. This could've been Pong pong.

2.

Pong::enable2D()

is illegal: enable2D isn't a static function, but a member (per-instance) function and thus you've to call it via an object, like pong.enable2D();.

3.

extern Pong pong;  // in the header
Pong pong;         // and both of these in the source
Pong Pong;

This is definitely wrong. What are you trying to do? Global or not, you can't declare an object named Pong of type Pong as explained above (1). Also you perhaps don't want a global but just an automatic variable in the main function.

Errors

error C2143: syntax error : missing ';' before '.'

If the compiler doesn't have the declaration of an identifier that it'll be unable to accept the token . and will be expecting a ; instead.

error C2248: 'Pong::height' : cannot access private member declared in class 'Pong'

This is as clear as C++ compiler errors get to be.

Please read a good C++ beginner book and get such logistical issues out of your way. Internet is not a good teacher, it's a good reference library, but it can't replace a good book mostly.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Thanks for the help! I'm mostly just left with some "undeclared identifier" errors that I've gotta figure out how to get rid of. – HiTechRedneck3 Nov 20 '14 at 09:34
  • In point 2., I am sure you meant `enable2D()` instead of `enable2()` – CinCout Nov 20 '14 at 10:12
  • @HiTechRedneck3 Welcome to StackOverflow! If the answer is helpful for you, you can "accpet" it by clicking 'check button', which is next to the body of the answer. – ikh Nov 20 '14 at 10:50
  • All of my errors seem to concentrated around lines 25-28 in my PongGameTest file, but I can't figure out how to correct them. – HiTechRedneck3 Nov 20 '14 at 10:52
  • The compiler is unable to see the variable `pong`. You should do `Pong pong;` inside the `main` function. – legends2k Nov 20 '14 at 10:56
  • I did this and am now only left with one error at the line that reads "glutTimerFunc(pong.interval, pong.update, 0);" in my PongGameTest file. The specific error is occurring on the "." in "pong.update". Any ideas? – HiTechRedneck3 Nov 20 '14 at 11:07
  • Isn't `update` a function? How do you call a function? What you're doing is referring to it like a variable. – legends2k Nov 20 '14 at 11:13
  • I've tried calling it several different ways, but keep getting errors, I know in my definition of update() it's passed an int value, but not sure what needs to be passed to it here... – HiTechRedneck3 Nov 20 '14 at 11:20