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

Author Topic: Passing "App" to my class's constructor? (solved!)  (Read 4797 times)

0 Members and 1 Guest are viewing this topic.

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Passing "App" to my class's constructor? (solved!)
« on: August 21, 2011, 06:46:37 am »
Inside of my player.h file:
Code: [Select]

class Player
{
private:
sf::RenderWindow App;
                ...
        //
        public:
Player(sf::RenderWindow App);
...
//
};


Inside of my player.cpp file:
Code: [Select]

Player::Player(sf::RenderWindow getApp)
{
App = getApp;
        ...
}


Inside of my main.cpp file:
Code: [Select]

int main(int argc, char** argv)
{
sf::RenderWindow App(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP), "Hey There!");

Player player(App);
...
}


However, a large amount of issues are reported:
Code: [Select]

In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Window.hpp:40:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:32,
                 from player.h:1,
                 from player.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp: In member function 'sf::Window& sf::Window::operator=(const sf::Window&)'
:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCo
pyable&)' is private
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Window/Window.hpp
:57:1: error: within this context
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:39:0,
                 from player.h:1,
                 from player.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp: In member function 'sf::RenderWindow& sf::RenderWindow::operator=(cons
t sf::RenderWindow&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp:44:1: note: synthesized method 'sf::Window& sf::Window::operator=(const
 sf::Window&)' first required here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics/RenderTexture.hpp:32:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:38,
                 from player.h:1,
                 from player.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp: In member function 'sf::RenderTarget& sf::RenderTarget::operator=(const s
f::RenderTarget&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCo
pyable&)' is private
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderTa
rget.hpp:48:1: error: within this context
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:37:0,
                 from player.h:1,
                 from player.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp: In member function 'sf::Renderer& sf::Renderer::operator=(const sf::Rende
rer&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCo
pyable&)' is private
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/Renderer
.hpp:49:1: error: within this context
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/Renderer
.hpp:49:1: error: non-static reference member 'sf::RenderTarget& sf::Renderer::m
yTarget', can't use default assignment operator
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics/RenderTexture.hpp:32:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:38,
                 from player.h:1,
                 from player.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderTa
rget.hpp: In member function 'sf::RenderTarget& sf::RenderTarget::operator=(cons
t sf::RenderTarget&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderTa
rget.hpp:48:1: note: synthesized method 'sf::Renderer& sf::Renderer::operator=(c
onst sf::Renderer&)' first required here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:39:0,
                 from player.h:1,
                 from player.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp: In member function 'sf::RenderWindow& sf::RenderWindow::operator=(cons
t sf::RenderWindow&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp:44:1: note: synthesized method 'sf::RenderTarget& sf::RenderTarget::ope
rator=(const sf::RenderTarget&)' first required here
player.cpp: In constructor 'Player::Player(sf::RenderWindow)':
player.cpp:5:8: note: synthesized method 'sf::RenderWindow& sf::RenderWindow::op
erator=(const sf::RenderWindow&)' first required here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Window.hpp:40:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:32,
                 from main.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp: In copy constructor 'sf::Window::Window(const sf::Window&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is pri
vate
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Window/Window.hpp
:57:1: error: within this context
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:39:0,
                 from main.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp: In copy constructor 'sf::RenderWindow::RenderWindow(const sf::RenderWi
ndow&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp:44:1: note: synthesized method 'sf::Window::Window(const sf::Window&)'
first required here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics/RenderTexture.hpp:32:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:38,
                 from main.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp: In copy constructor 'sf::RenderTarget::RenderTarget(const sf::RenderTarge
t&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is pri
vate
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderTa
rget.hpp:48:1: error: within this context
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:37:0,
                 from main.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp: In copy constructor 'sf::Renderer::Renderer(const sf::Renderer&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/System/NonCopyabl
e.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is pri
vate
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/Renderer
.hpp:49:1: error: within this context
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics/RenderTexture.hpp:32:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:38,
                 from main.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderTa
rget.hpp: In copy constructor 'sf::RenderTarget::RenderTarget(const sf::RenderTa
rget&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderTa
rget.hpp:48:1: note: synthesized method 'sf::Renderer::Renderer(const sf::Render
er&)' first required here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/
SFML/Graphics.hpp:39:0,
                 from main.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp: In copy constructor 'sf::RenderWindow::RenderWindow(const sf::RenderWi
ndow&)':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/SFML/Graphics/RenderWi
ndow.hpp:44:1: note: synthesized method 'sf::RenderTarget::RenderTarget(const sf
::RenderTarget&)' first required here
main.cpp: In function 'int main(int, char**)':
main.cpp:26:19: note: synthesized method 'sf::RenderWindow::RenderWindow(const s
f::RenderWindow&)' first required here
main.cpp:26:19: error:   initializing argument 1 of 'Player::Player(sf::RenderWi
ndow)'


What could the issue be?  Thanks for any assistance!

Also, the ... symbolizes that I took out pieces of irrelevant code.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Passing "App" to my class's constructor? (solved!)
« Reply #1 on: August 21, 2011, 07:00:04 am »
You can't copy windows, which is what you're doing. Pass a reference to the window instead.
I use the latest build of SFML2

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Passing "App" to my class's constructor? (solved!)
« Reply #2 on: August 21, 2011, 09:14:46 am »
Quote from: "OniLink10"
You can't copy windows, which is what you're doing. Pass a reference to the window instead.


Thanks for the reply!  Here's what I'm currently trying:

player.h file
Code: [Select]

class Player
{
private:
sf::RenderWindow App;
..
//
public:
Player(sf::RenderWindow &getApp);
...
//
};


player.cpp
Code: [Select]

Player::Player(sf::RenderWindow &getApp)
{
App = getApp;
...
}


main.cpp
Code: [Select]

Player player(&App);


However, it appears that essentially the same message from the original post is returned.  What could the issue be?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Passing "App" to my class's constructor? (solved!)
« Reply #3 on: August 21, 2011, 09:30:35 am »
Quote from: "fatum"
Quote from: "OniLink10"
You can't copy windows, which is what you're doing. Pass a reference to the window instead.

What could the issue be?
The issue is right here
player.h file
Code: [Select]

class Player
{
private:
sf::RenderWindow App;
..
//
public:
Player(sf::RenderWindow &getApp);
...
//
};

Change the sf::RenderWindow App to sf::RenderWindow &App, and initialize it with an initialization list.
I use the latest build of SFML2

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Passing "App" to my class's constructor? (solved!)
« Reply #4 on: August 21, 2011, 10:39:53 am »
Thanks for the help again!  Could you please elaborate on what you mean by initialization lists?  I've found a few articles, and it looked like they were pointing towards something like this:

player.h
Code: [Select]

public:
Player(sf::RenderWindow &getApp);
Player();


player.cpp
Code: [Select]

Player::Player(sf::RenderWindow &getApp) : App(&App)


I'm sure that what I'm currently trying to do looks pretty ridiculous, it certainly doesn't feel right.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6278
  • Thor Developer
    • View Profile
    • Bromeon
Passing "App" to my class's constructor? (solved!)
« Reply #5 on: August 21, 2011, 11:26:19 am »
Initializer lists are a language feature to initialize member variables in the constructor. You should always prefer them over assignments in the constructor body. Sometimes they are unavoidable (for references, constants, not default-constructible objects).

Do you have a good C++ book, such as the C++ primer, Accelerated C++ or Thinking in C++? If not, you should really read one, it is certainly worth the time and money.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Passing "App" to my class's constructor? (solved!)
« Reply #6 on: August 21, 2011, 11:56:29 am »
I was able to eliminate all of the errors.  Currently I'm trying with:

player.h
Code: [Select]

public:
void update(sf::RenderWindow &App);


player.cpp
Code: [Select]

Player::Player(float getX, float getY, sf::Texture getImg)
{
sf::Sprite self(getImg);
...
}

void Player::update(sf::RenderWindow &App)
{
App.Draw(self);
}


main.cpp
Code: [Select]

App.Clear();
player.update(App);
App.Display();


The "App.Draw(self);" is causing the issue.  Without it, the code compiles without any errors.  Here are the errors returned:
Code: [Select]

player.cpp: In member function 'void Player::update(sf::RenderWindow&)':
player.cpp:37:15: error: no matching function for call to 'sf::RenderWindow::Draw(sf::Texture&)'


Also, I do have a really interesting C++ book that I like to read through sometimes.  Do you have any recommendations?

jkalmeij

  • Newbie
  • *
  • Posts: 8
    • View Profile
Passing "App" to my class's constructor? (solved!)
« Reply #7 on: August 21, 2011, 03:05:27 pm »
Your error happens to me sometimes when I'm typing code on auto-mode.

in player.cpp:
Code: [Select]
 sf::Sprite self(getImg);


This declares a local sprite, named self.

Now I am guessing you have a member of your player class called self declared somewhere (Why else would you call App.Draw(self)?). The compiler error hints that the type of this class is a sf::Texture too (while it's probably meant to be a sf::Sprite?).

The code is too incomplete to come up with a solution. But just remember that to call a non-default constructor of a member variable you have to call it in the initialization list.
You can also just use the SetTexture function that is provided.

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Passing "App" to my class's constructor? (solved!)
« Reply #8 on: August 21, 2011, 05:19:09 pm »
Quote from: "jkalmeij"
Your error happens to me sometimes when I'm typing  (while it's probably meant to be a sf::Sprite?).


Wow, that was it.  lol I can't believe I overlooked that I gave self the wrong type in my player.h file.  There currently are not any errors, however nothing is displayed.  Here's my Player's constructor:

Code: [Select]

Player::Player(float getX, float getY, sf::Texture getImg)
{
sf::Sprite self(getImg);
x = getX;
y = getY;

self.SetPosition(getX, getY);
}


(I also tried using .SetTexture(); as well)

main.cpp
Code: [Select]

sf::Texture img_player;

if (!img_player.LoadFromFile("res/img/char/player.png"))
{
return EXIT_FAILURE;
}

Player player(20, 20, img_player);

...

while (App.IsOpened())
{

App.Clear();
player.update(App);
App.Display();
}


Also, if it's helpful:
player.h
Code: [Select]

#include "SFML/Graphics.hpp"
#include <iostream>

class Player
{
private:
float x, y, xs, ys, speed, fric;
bool right, left;
sf::Sprite self;
//
public:
Player(float getX, float getY, sf::Texture getImg);
void input(sf::Event event);
void update(sf::RenderWindow &App);
//
};


If I replaced it with this bit:
main.cpp
Code: [Select]

sf::Texture img_player;

if (!img_player.LoadFromFile("res/img/char/player.png"))
{
return EXIT_FAILURE;
}

sf::Sprite spr_player(img_player);
spr_player.SetPosition(10.0f, 10.0f);
while (App.IsOpened())
{

App.Clear();
//player.update(App);
App.Draw(spr_player);
App.Display();
}


The sprite is displayed on the screen.  What could I be doing wrong?

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Passing "App" to my class's constructor? (solved!)
« Reply #9 on: August 21, 2011, 06:22:47 pm »
jkalmeij already pointed the error.
You defined the local 'sf::Sprite self' inside the constructor and initialized it instead of the member variable 'self'.

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Passing "App" to my class's constructor? (solved!)
« Reply #10 on: August 21, 2011, 06:50:48 pm »
If anyone is looking for a solution to a similar problem, here's what I implemented:

player.h
Code: [Select]

public:
void update(sf::Sprite &spr_player);
...
//


player.cpp
Code: [Select]

void Player::update(sf::Sprite &spr_player)
{


main.cpp
Code: [Select]

bool setup()
{
if (!img_player.LoadFromFile("res/img/char/player.png"))
{
return false;
}

spr_player.SetTexture(img_player);

return true;
}

int main(int argc, char** argv)
{
...
if (setup() == false)
{
return EXIT_FAILURE;
}

Player player(20, 20);

while (App.IsOpened())
{
..
App.Clear();
App.Draw(spr_player);

player.update(spr_player);
App.Display();
}

return EXIT_SUCCESS;
}


I'm also probably going to create an array that holds all of the sprites that need displaying, and call App.Draw(spriteBatch[index]); in a for loop.

  • Guest
Passing "App" to my class's constructor? (solved!)
« Reply #11 on: August 22, 2011, 11:43:55 am »
Thank you for your share, I really liked your article, I think you said something very meaningful. I will often come to your published article, hope that something is to see something different each time  


Cheap Polo Shirts
Women's RL Polo Shirts
slimming tea

 

anything