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

Author Topic: Cannot Access Private Member in class "sf::NonCopyable&  (Read 2959 times)

0 Members and 1 Guest are viewing this topic.

ascii

  • Newbie
  • *
  • Posts: 8
    • AOL Instant Messenger - 2910+fake+st
    • View Profile
Cannot Access Private Member in class "sf::NonCopyable&
« on: June 19, 2011, 07:31:07 pm »
Hey guys!  I started learning SFML and was screwing around trying to get a movement class to work, but I keep getting a compiler error I just don't understand.  The error is:
'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>        c:\sfml-1.6\include\sfml\system\noncopyable.hpp(57) : see declaration of 'sf::NonCopyable::NonCopyable'
1>        c:\sfml-1.6\include\sfml\system\noncopyable.hpp(41) : see declaration of 'sf::NonCopyable'

My definition of the Move class is as follows:
Code: [Select]

#pragma once
#include<SFML/Graphics.hpp>

enum Direction { up, down, left, right };

class CMove
{
public:
CMove(sf::Shape& obj)
: object(&obj)
{ }

void WalkLeft() // walk left
{
dir = left;
Walk(dir);
}
void WalkRight() // walk right
{
dir = right;
Walk(dir);
}
void WalkUp() // walk up
{
dir = up;
Walk(dir);
}
void WalkDown() // walk down
{
dir = down;
Walk(dir);
}
private:
Direction dir; // the direction to move in
sf::Shape* object; // pointer to the object to move
void Walk(Direction Dir) const; // move the object
};

// move the player in the direction specified in dir
void CMove::Walk(Direction dir) const
{
switch(dir)
{
case up:
object->Move(0, -2);
case down:
object->Move(0, 2);
case left:
object->Move(2, 0);
case right:
object->Move(-2, 0);
}
return;
}


Thanks!

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Cannot Access Private Member in class "sf::NonCopyable&
« Reply #1 on: June 19, 2011, 07:46:14 pm »
You're trying to copy something that you're now allowed to but I think the error is not in this particular part of the code. Look at your compiler error message : it should contains a file and a line number where the error lies.

(All classes inheriting from sf::NonCopyable are, well, non-copyable. You need to pass to function such objects by reference and not value.)
SFML / OS X developer

ascii

  • Newbie
  • *
  • Posts: 8
    • AOL Instant Messenger - 2910+fake+st
    • View Profile
Cannot Access Private Member in class "sf::NonCopyable&
« Reply #2 on: June 19, 2011, 07:54:41 pm »
Bleh, stupid me.  In my main function when I was declaring my variable for storing input I forgot to make it a const reference, so it was trying to make a copy of the input I guess  -_-  Fixed now, however now for some reason the player can only walk right and down, I'll have to work on that :/

ascii

  • Newbie
  • *
  • Posts: 8
    • AOL Instant Messenger - 2910+fake+st
    • View Profile
Cannot Access Private Member in class "sf::NonCopyable&
« Reply #3 on: June 19, 2011, 08:34:22 pm »
Any help on this?  I can't figure out why the hell only right and down are working.

If it helps here's my main function:
Code: [Select]

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

void handleMovement(sf::Shape& object, const sf::Input& Input)
{
CMove Move(object);
bool leftKey = Input.IsKeyDown(sf::Key::Left);
bool rightKey = Input.IsKeyDown( sf::Key::Right);
bool upKey = Input.IsKeyDown(sf::Key::Up);
bool downKey = Input.IsKeyDown(sf::Key::Down);
if(leftKey)
Move.WalkLeft();
if(rightKey)
Move.WalkRight();
if(upKey)
Move.WalkUp();
if(downKey)
Move.WalkDown();
}

int main(void)
{
sf::RenderWindow App(sf::VideoMode(800, 640, 32), "SFML Window");
App.SetFramerateLimit(60);

sf::Shape rect = sf::Shape::Rectangle(300, 300, 350, 350, sf::Color(200, 200, 200));
while(App.IsOpened())
{
sf::Event Event;
const sf::Input& Input = App.GetInput();
while(App.GetEvent(Event))
{
// handle movement
handleMovement(rect, Input);

if(Event.Type == sf::Event::Closed)
App.Close();
}

App.Clear();
App.Draw(rect);
App.Display();
}


return EXIT_SUCCESS;
}

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Cannot Access Private Member in class "sf::NonCopyable&
« Reply #4 on: June 19, 2011, 08:40:36 pm »
I would say it's your code in Up and left are wrong as I can't see anything wrong in your main function. So could you give us the Move code?

Also it looks like you have made a poltergeist class, those are considered anti-patterns just so you know.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

ascii

  • Newbie
  • *
  • Posts: 8
    • AOL Instant Messenger - 2910+fake+st
    • View Profile
Cannot Access Private Member in class "sf::NonCopyable&
« Reply #5 on: June 19, 2011, 08:47:02 pm »
EDIT: Fixed it!  Nevermind!

The move code is in the original post.  Here it is again:
Code: [Select]

enum Direction { left, right, up, down };

class CMove
{
public:
CMove(sf::Shape& obj)
: object(&obj)
{ }
void WalkLeft() // walk left
{
dir = left;
Walk(dir);
}
void WalkRight() // walk right
{
dir = right;
Walk(dir);
}
void WalkUp() // walk up
{
dir = up;
Walk(dir);
}
void WalkDown() // walk down
{
dir = down;
Walk(dir);
}
private:
Direction dir; // the direction to move in
sf::Shape* object; // pointer to the object to move
void Walk(Direction dir) const; // move the object
};

// move the player in the direction specified in dir
void CMove::Walk(Direction dir) const
{
switch(dir)
{
case left:
object->Move(-5, 0);
case right:
object->Move(5, 0);
case up:
object->Move(0, -5);
case down:
object->Move(0, 5);
}
return;
}


As for it being a poltergeist class I moved the instantiation of the Move object outside of the main loop so it's only instantiated once.  The main function looks like this now:
Code: [Select]

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

void handleMovement(const sf::Input& Input, CMove* Move) // handle movement of player
{
bool leftKey = Input.IsKeyDown(sf::Key::Left);
bool rightKey = Input.IsKeyDown( sf::Key::Right);
bool upKey = Input.IsKeyDown(sf::Key::Up);
bool downKey = Input.IsKeyDown(sf::Key::Down);
if(leftKey)
Move->WalkLeft();
if(rightKey)
Move->WalkRight();
if(upKey)
Move->WalkUp();
if(downKey)
Move->WalkDown();
}

int main(void)
{
sf::RenderWindow App(sf::VideoMode(800, 640, 32), "SFML Window");
App.SetFramerateLimit(60);

sf::Shape rect = sf::Shape::Rectangle(300, 300, 350, 350, sf::Color(200, 200, 200));
CMove Move(rect);
CMove* pMove = &Move;
while(App.IsOpened())
{
sf::Event Event;
const sf::Input& Input = App.GetInput();
while(App.GetEvent(Event))
{
// handle movement
handleMovement(Input, pMove);

if(Event.Type == sf::Event::Closed)
App.Close();
}

App.Clear();
App.Draw(rect);
App.Display();
}


return EXIT_SUCCESS;
}

azurfire

  • Newbie
  • *
  • Posts: 6
    • View Profile
Cannot Access Private Member in class "sf::NonCopyable&
« Reply #6 on: June 23, 2011, 03:18:58 am »
You're missing the break statements in your switch block. It should look like this:

Code: [Select]
  case left:
      object->Move(-5, 0);
      break;
   case right:
      object->Move(5, 0);
      break;
   case up:
      object->Move(0, -5);
      break;
   case down:
      object->Move(0, 5);
      break;


Otherwise, if you press left, it will also run the code for right, up, and down.

 

anything