Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using a window though multiple classes  (Read 2947 times)

0 Members and 1 Guest are viewing this topic.

Excellent

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Using a window though multiple classes
« on: March 16, 2014, 12:58:45 am »
Hello, I found this cool library after months of trying to find a c++ library that I could understand (c++ is my first language) and i've been playing around with it seeing what I can create

Sadly I have run into a problem that I don't know how to solve weather it be due to my lack of c++ experience or with the library itself. My problem is I can't figure out how to use a window that is declared in Class A  and use it in Class B, for example.

 class A
{
public:
                    A(); // Constuctor for A
 sf::RenderWindow mWindow;
};
A::A()
: mWindow(sf::VideoMode(1200, 720))
{
}
class B : public A
{
void drawSprite
sf::Sprite someSprite;
};
void B::drawSprite()
{
mWindow.draw(someSprite);
// I know there is nothing in someSpite but for the sake of readability lets say there was
}
 

When I try to create the "drawSprite" function in my code the IDE says that mWindow is an unknown type name. I wish I knew more about classes but my c++ knowledge is limited to a few beginner books. I know this may seem like a really dumb problem but if anyone would be willing to explain to me why this is not working I would be greatly appreciative.

Thanks  :)
« Last Edit: March 16, 2014, 03:07:32 am by Excellent »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Using a window though multiple classes
« Reply #1 on: March 16, 2014, 02:25:41 am »
Well I typed "c++ classes" into google and in the first result I found this little gem.

Quote
By default, all members of a class declared with the class keyword have private access for all its members.

Now lets see if you can figure out the problem.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Excellent

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Using a window though multiple classes
« Reply #2 on: March 16, 2014, 02:58:14 am »
So if a even if I declare "mWindow" in A's constructor (which I placed in the "public" area) it still makes "mWindow" private thus making the inheritance not work because it only inherits public members?

So a solution to this would be to create a "sf::Window mWindow" inside the "public" area of A then in the constructor make a "sf::Window mWindow2" then render it and copy it to "mWindow" thus making it available  for B to have stuff rendered onto it?

Again sorry that I am unsure, I assumed placing the constructor in the "public" would give B access to it

Edit: facepalm on myself, I placed the "public:" in the wrong spot on my original post but it is the right spot now so it matches with my code, but I still dont understand why the IDE gives me an error
« Last Edit: March 16, 2014, 03:10:32 am by Excellent »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Using a window though multiple classes
« Reply #3 on: March 16, 2014, 04:08:00 am »
Edit: facepalm on myself, I placed the "public:" in the wrong spot on my original post but it is the right spot now so it matches with my code, but I still dont understand why the IDE gives me an error

Well maybe you should post the error then  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Excellent

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Using a window though multiple classes
« Reply #4 on: March 16, 2014, 04:22:21 am »
Thanks for your help zsb, as I stated earlier it says that mWindow is an unknown type name, I've checked and mWindow is in class A AND it is under the "public" namespace. Im using Xcode on a mac, if that helps at all, im guessing what it means by unknown type name is that it cannot find where I declared "mWindow" but I have no idea why.

Before I was using sf::RenderWindow "mWindow" in class 'A' but i've tried just sf::Window mWindow as well and it still telling me that class B doesn't know what 'mWindow' is (unknown type name).

As far as I can tell i've followed the tutorial to the tee and I can draw stuff to mWindow as long as it is with a function that is in class A

Sorry for wasting your time, I wish i knew if this is a problem because I don't have a proper understanding of c++ or because I don't have a proper understanding of SFML, thanks again atleast i'm learning  :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Using a window though multiple classes
« Reply #5 on: March 16, 2014, 10:39:12 am »
It's indeed a lack of C++ knowledge, SFML is not different than any other library here. Do you have a good C++ book? You can't learn this language by doing ;)

Have you included the required headers?
Can you show a minimal complete example that reproduces the problem, so we can stop guessing?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Excellent

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Using a window though multiple classes
« Reply #6 on: March 16, 2014, 04:48:41 pm »
Ok, I WAS trying to keep the code as simple as possible but ill show my exact code so you can get an idea of what I am trying to do

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

class game
{
public:
                                game();
// Irellivant functions that draw stuff to mWindow I made are here (they work!)
sf::RenderWindow mWindow;
// Irrelevant sf::Textures and such are here
};
game::game()
: mWindow(sf::VideoMode(1200, 720), Header)
{
// Load up all the textures
}

class MainMenu : public game // inherits all of the members of game
{
                        MainMenu();
// More irellivant functions
void drawButton();
sf::Sprite button;
sf::Texture buttonTexture
};

MainMenu::MainMenu()
{
// For the sake of simplicity, lets say I loaded the texture and assigned it to the "button" object
}

MainMenu::drawButton()
{
mWindow.draw(button);       // this is the exact spot where the error "mWindow is an unknown type name" appears
}
 

This is where the IDE says it dosent know what mWindow is, I have read all the chapters in my book on classes and I deduced that since i inherited all of "game" public members I should also get "sf::RenderWindow mWindow"

Edit: AH HA! After flipping though my book for a few hours I remembered that the parent constructor is not automatically used! So as far as I could guess I was trying draw something onto something that didn't exist (as it was not initialized).

Well thanks for all your help guys and thanks for your patience
« Last Edit: March 16, 2014, 07:26:00 pm by Excellent »

 

anything