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

Author Topic: Can't place variables outside function  (Read 1519 times)

0 Members and 1 Guest are viewing this topic.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Can't place variables outside function
« on: September 21, 2015, 12:29:34 pm »
Hello

I'm encountering a small problem.
I've got all my variables placed in a function, who's in a class. That's working. But when I place my variables outside the function still in the class, I get the error: Expected a ')' AND Type identifier expected.

Code:
Working:
class a {
public:
void function() {
sf::RenderWindow window(sf::VideoMode(800, 600) "Title");
}
}

Not working:
class a {
public:
sf::RenderWindow window(sf::VideoMode(800, 600) "Title");
void function() {
//
}
}

How to solve this?

Thanks for reading,
Niely

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can't place variables outside function
« Reply #1 on: September 21, 2015, 01:16:31 pm »
That's a basic C++ problem, nothing related to SFML, let alone to a binding. In the future, please use other platforms (C++ forums, StackOverflow) for this kind of question.

Declare the variable in the class without definining it, and initialize it in the constructor initializer list. Please don't just ask how and expect 1:1 code; do a bit of research. These are absolute basics that you must know when you want to use a library like SFML.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Klaim

  • Full Member
  • ***
  • Posts: 137
    • View Profile
Re: Can't place variables outside function
« Reply #2 on: September 22, 2015, 12:17:43 am »
Nexus is correct for C++03 and before. However if you use C++11 (needs a flag on gcc and clang, automatic in Visual Studio 2015), restrictions have been lifted and this code is valid:
http://coliru.stacked-crooked.com/a/96f4c2a789640d65

I don't remember all the rules but I know that:

 1. It does not work the way you wrote it, it's forbidden to use directlyt he constructor through () with member initialization;
 2. You can use {} for member initialization (see previous link);
 3. You can use operator= if the type allows it, but it's better only when you deduce the value from a function. Here is a "bad" example : http://coliru.stacked-crooked.com/a/5bbb9f01e75951c2