Unresolved Externals Error in C++

33 views Asked by At

I'm coding a game of Pong and have run into some compiler errors that I can't seem to fix.

Here's 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);
    static void draw();
    void enable2D(int width, int height);
    void keyboard();
    void vec2_norm(float& x, float &y);
    void updateBall();
    void gameOverCheck();
    static 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;
};
Pong pong;

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

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 = 15;
    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) {
        std::cout << "Player 1 Wins!" << std::endl;
        pong.winner = 1;
    }
    else if(pong.p2Score == maxScore) {
        std::cout << "Player 2 Wins!" << std::endl;
        pong.winner = 2;
    }
}

void update(int value) {
   keyboard();

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

My main 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;

int _tmain(int argc, char** argv) {
    //Pong pong;
    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 here are the errors I'm getting when I try to compile:

1>PongGameTest.obj : error LNK2005: "class Pong pong" (?pong@@3VPong@@A) already defined in PongGame.obj
1>PongGameTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Pong::enable2D(int,int)" (?enable2D@Pong@@QAEXHH@Z) referenced in function _wmain
1>PongGameTest.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Pong::update(int)" (?update@Pong@@SAXH@Z) referenced in function _wmain
1>PongGameTest.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Pong::draw(void)" (?draw@Pong@@SAXXZ) referenced in function _wmain
1>C:\Users\HiTechRedneck\Desktop\Fall 2014\Object-Oriented Programming\Game\pongGame\Debug\pongGame.exe : fatal error LNK1120: 3 unresolved externals

I know that the first error says that my "Pong pong" object is already defined, however when I delete it from my .h file (leaving it in main), I get over 100 errors, most of which are "undeclared identifier" errors. If I leave it in my .h file and delete it from main, I'm left with those same unresolved externals that I included here.

In case it's important, I'm writing this in Visual Studio 2010 Professional.

Update I managed to clear most of the errors but am left with one that states:

1>PongGameTest.obj : error LNK2005: "class Pong pong" (?pong@@3VPong@@A) already defined in PongGame.obj
1>C:\Users\HiTechRedneck\Desktop\Fall 2014\Object-Oriented Programming\Game\pongGame\Debug\pongGame.exe : fatal error LNK1169: one or more multiply defined symbols found

Any ideas what I can do to correct these errors?

0

There are 0 answers