Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:28:38+00:00 2026-06-09T04:28:38+00:00

I’m a new person to C++, but i have learned a little Java and

  • 0

I’m a new person to C++, but i have learned a little Java and I/m working with SDL for the first time at the same time but my problem is I’m trying to create an object (paddle) p1 and p2.

this

#ifndef PONG_H_
#define PONG_H_
#undef main
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>



class paddle{
private:
    int y;
    int yv;
    int x;
public:

    paddle(){
    y = 0;
    yv = 0;
    x = 0;
    }

    paddle(int ex, int why, int vel){
        y = why;
        yv = vel;
        x = ex;
        }

    void setY(int ycoord) {y = ycoord;}
    void setV(int velocity){ yv = velocity;}
    void setX(int xcoord) {x = xcoord;}
    int getX() {return x;}
    int getY() {return y;}
    int update(){y += yv; return y;}




};


class Pong { //pong class where everythin gets its name and a purpose.

private:

    SDL_Surface * Paddle;
    SDL_Surface * Ball; //SDL_Surface pointer for Ball
    SDL_Surface * screen;   //SDL_Surface  pointer to backbuffer

public:
    int Running;
    Pong(); //eh..

    int OnExecute(); //another function returning a value.
    int w; //width of screen
    int h; //height of screen
    bool OnInit();
    void OnEvent(SDL_Event* Event);
    void OnLoop();
    void OnRender();
    void OnCleanup();


    void redraw( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Make a temporary rectangle to hold the offsets
    SDL_Rect offset; //Give the offsets to the rectangle
    offset.x = x; offset.y = y;
    SDL_BlitSurface( source, NULL, destination, &offset );//Blit the surface
    offset.x =0; offset.y=0; //resets the offsets sicne they apaprently dont reset per use.
    };

};


#endif /* PONG_H_ */

is my header file, it contains the main functions of pong class also but I omitted those to make things less convoluted. Let me know if those are needed for a good answer.

and i declare the objects here…

#include <iostream>
#include <SDL/SDL.h>

#include "Pong.h"
paddle p1;
paddle p2;

Pong::Pong(){
h = 768;
w = 1024;
screen = NULL;
Paddle = NULL;
Ball = NULL;

Running = true;
atexit(SDL_Quit);

}

int Pong::OnExecute() {
    if(OnInit() == false)
        return-1;
    SDL_Event Event;
    while(Running) {
        while(SDL_PollEvent(&Event)) {
            OnEvent(&Event);
        }

        OnLoop();
        OnRender();
    }
    OnCleanup();
return 0;
}

int main( int argc, char* args[]) {
    Pong theApp;
    return theApp.OnExecute();
}

but what sort of makes it difficult is i use it in…here:

#include "Pong.h"
void Pong::OnEvent(SDL_Event* Event) {
    if(Event->type == SDL_QUIT)
        Running = false;
    switch(Event->type)
    {

        case SDL_KEYDOWN: //look for key holds
            switch(Event->key.keysym.sym) //check key values and changes coords respectiely
            {
            default:
                break;
            case SDLK_UP:
                p2.setV(-2);
                break;

            case SDLK_DOWN:
                p2.setV(2);
                break;

            case SDLK_w:
                p1.setV(-2);
                break;

            case SDLK_s:
                p1.setV(2);
                break;

            case SDLK_ESCAPE:
            Running = false;
                break;
            }
            break;
        case SDL_KEYUP:
            switch(Event->key.keysym.sym)
            {
            default:
                break;
                case SDLK_UP:
                    p2.setV(0);
                    break;

                case SDLK_DOWN:
                    p2.setV(0);
                    break;

                case SDLK_w:
                    p1.setV(0);
                    break;

                case SDLK_s:
                    p1.setV(0);
                    break;
            }
            break;
        break;//break of Key holding event check
    }
}

Its hard for me to ask the question because I don’t know where to begin. I tried many different weird things and I’m just lost now. If you need any more info I’ll be happy to provide.

void Pong::OnLoop(){

    if(p1.getY()<=0){
        p1.setV(0);
        p1.setY(0);
    }
    if(p2.getY()<=0){
        p2.setV(0);
        p2.setY(0);
    }
    if(p1.getY()>=h - Paddle->h){
        p1.setV(0);
            p1.setY(h - Paddle->h);
        }
    if(p2.getY()>=h - Paddle->h){
        p2.setV(0);
            p2.setY(h - Paddle->h);
        }
}

..\src\Pong_OnLoop.cpp:11:5: error: ‘p1’ was not declared in this scope

..\src\Pong_OnLoop.cpp:15:5: error: ‘p2’ was not declared in this scope

..\src\Pong_OnLoop.cpp:19:5: error: ‘p1’ was not declared in this scope

..\src\Pong_OnLoop.cpp:23:5: error: ‘p2’ was not declared in this scope

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:28:40+00:00Added an answer on June 9, 2026 at 4:28 am

    Since you haven’t provided a clear question, it’s difficult to provide the right answer, but my guess is that you can’t access p1 and p2 in the OnEvent-/OnLoop-Method.

    Is it absolutely necessary to let the two paddle-objects live in the global namespace? Because in this scenario I think it would be best to have 2 paddle-members as part of the Pong class, also I would use initialization lists like so:

    class Pong {
    private:
    
        SDL_Surface * Paddle;
        SDL_Surface * Ball; //SDL_Surface pointer for Ball
        SDL_Surface * screen;   //SDL_Surface  pointer to backbuffer
    
        paddle p1;
        paddle p2;
    
    public:
        Pong() :
            h(768),
            w(1024),
            screen(NULL),
            Paddle(NULL),
            Ball(NULL),
            Running(true),
            p1(10, 384, 0),
            p2(1014, 384, 0) {
            atexit(SDL_Quit);
        };
    
        // ...
    }; 
    

    You might not even need all initializers depending on the optimization settings and the default memory initialization but that’s not the point. This way you should have access to p1 and p2 in all of Pongs own methods. Bo Perssons answer may work as well, but I think this is a more object- and thus C++-oriented approach.

    On a side note: You said that the header file contains some more methods – seeing that your constructor is implemented in the cpp-file and the redraw-Method in the header-file you might want to learn a bit more about inlining. It won’t be much of an issue in this example but it’s usually best practice to move the more complex methods to the translation units (cpp-files), especially when creating libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.