Yes, I'm still trying to make pong, and yes, I do have another bug.

Started by
2 comments, last by Mussi 11 years, 7 months ago
Here's the code:
Paddle Class Header:

#include <SFML/Graphics.hpp>
class Paddle
{
public:
void Draw(sf::RenderWindow Window);
void Update(sf::RenderWindow Window);
void Initialize(float speedx, bool isplayercontrolled, sf::Image Image);
private:
sf::FloatRect Collision;
sf::Image PaddleImage;
sf::Sprite PaddleSprite;
bool HasAI;
float speedx;
};

Paddle Class Source:

#pragma once
#include <SFML/Graphics.hpp>
#include "Paddle.h"
void Paddle::Initialize(float speedx, bool isplayercontrolled, sf::Image Image)
{
this->speedx = speedx;
this->HasAI = isplayercontrolled;
this->PaddleImage = Image;
PaddleSprite.SetImage(Image);
}
void Paddle::Draw(sf::RenderWindow Window)
{
Window.Draw(PaddleSprite);
return;
}
void Paddle::Update(sf::RenderWindow Window)
{
if (sf::Key::Left) PaddleSprite.Move(-speedx * Window.GetFrameTime(), 0);
else if (sf::Key::Right) PaddleSprite.Move(speedx * Window.GetFrameTime(), 0);
Collision.Top = PaddleSprite.GetPosition().y;
Collision.Left = PaddleSprite.GetPosition().x;
Collision.Bottom = Collision.Top + PaddleImage.GetHeight();
Collision.Right = Collision.Left + PaddleImage.GetWidth();
}

Main(Testing the Paddle Class out):

// Pong.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Paddle.cpp"

int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "Pong");
App.SetFramerateLimit(30);
Paddle Paddle1;
sf::Image Image1;
bool AI = false;
Paddle1.Initialize(200, AI, Image1);

while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
Paddle1.Update(App);
Paddle1.Draw(App);
App.Clear();
App.Display();
}
return 0;
}

It gives me an error message for:

1>------ Build started: Project: Pong, Configuration: Debug Win32 ------
1> stdafx.cpp
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(6): error C2653: 'Paddle' : is not a class or namespace name
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(8): error C2355: 'this' : can only be referenced inside non-static member functions
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(8): error C2227: left of '->speedx' must point to class/struct/union/generic type
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(9): error C2355: 'this' : can only be referenced inside non-static member functions
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(9): error C2227: left of '->HasAI' must point to class/struct/union/generic type
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(10): error C2355: 'this' : can only be referenced inside non-static member functions
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(10): error C2227: left of '->PaddleImage' must point to class/struct/union/generic type
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(11): error C2065: 'PaddleSprite' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(11): error C2228: left of '.SetImage' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(14): error C2653: 'Paddle' : is not a class or namespace name
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(16): error C2065: 'PaddleSprite' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(20): error C2653: 'Paddle' : is not a class or namespace name
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(22): error C2065: 'PaddleSprite' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(22): error C2228: left of '.Move' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(23): error C2065: 'PaddleSprite' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(23): error C2228: left of '.Move' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(24): error C2065: 'Collision' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(24): error C2228: left of '.Top' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(24): error C2065: 'PaddleSprite' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(24): error C2228: left of '.GetPosition' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(24): error C2228: left of '.y' must have class/struct/union
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(25): error C2065: 'Collision' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(25): error C2228: left of '.Left' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(25): error C2065: 'PaddleSprite' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(25): error C2228: left of '.GetPosition' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(25): error C2228: left of '.x' must have class/struct/union
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(26): error C2065: 'Collision' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(26): error C2228: left of '.Bottom' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(26): error C2065: 'Collision' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(26): error C2228: left of '.Top' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(26): error C2228: left of '.GetHeight' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(27): error C2065: 'Collision' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(27): error C2228: left of '.Right' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(27): error C2065: 'Collision' : undeclared identifier
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(27): error C2228: left of '.Left' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\tom\documents\visual studio 2010\projects\pong\paddle.cpp(27): error C2228: left of '.GetWidth' must have class/struct/union
1> type is ''unknown-type''

What is going on? it gives me an error on every line of code for .cpp all saying THE EXACT SAME THING: .(Name of Function) Must Have a Class/Struct/Union? WTF!

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Advertisement
First, the [font=courier new,courier,monospace]#pragma once[/font] belongs in the header file ([font=courier new,courier,monospace]#pragma once[/font] should not go in a .cpp file!). Second, you shouldn't be includin[font=arial,helvetica,sans-serif]g Paddle.cpp in Main.cpp. Rath[/font]er, you should compile Paddle.cpp all by itself, and Main.cpp all by itself, and then link them together (Visual Studio will automatically do this for you if you just add both .cpp files to your project). Then you can just include Paddle.h in Main.cpp.

Try fixing those things and then come back if you have more problems.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
How can I compile both of them by themselves? (#I'mANoob)

--------EDIT: FIXED------
Okay, so the problem turned out to be me including the .cpp instead of the .h, and I had to change from passing sf::RenderWindow as a value to passing it as a reference because of sf::NonCopyable. But overall, it's fixed.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

You don't have to justify asking a 'noob' question, that's what this forum is for. Don't hesitate to ask questions when you run into more trouble :).

This topic is closed to new replies.

Advertisement