trouble converting code to class

i was trying to convert main2() 's example code to classes. However i am not sure why i am having trouble creating window in the class?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

class Control{
    public:
        sf::Window window(sf::VideoMode(600,400), "Title");
        sf::Time time;
        sf::Clock clock;
        
        void run(){
            while (window.isOpen()){
                sf::Event event;
                while(window.pollEvent(event)){
                    if (event.type == sf::Event::Closed){
                        window.close();
                    }
                }
                time = clock.restart();
                window.display();
            }
        }
};

int main(){
    Control app;
    app.run();
}

int main2()
{
    sf::Window window(sf::VideoMode(800, 600), "My window");
    
    sf::Time time;
    
    sf::Clock clock;
    
    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }
            std::cout << event.type << std::endl;
        }
        
        time = clock.restart();
        //std::cout << time.asSeconds() << std::endl;
        
        
        window.display();
    }
}


error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
test.cpp:7:35: error: expected ‘)’ before numeric constant
   sf::Window window(sf::VideoMode(600,400), "Title");
                                   ^
test.cpp:7:35: error: expected ‘)’ before numeric constant
test.cpp:7:34: error: expected ‘;’ at end of member declaration
   sf::Window window(sf::VideoMode(600,400), "Title");
                                  ^
test.cpp:7:35: error: expected unqualified-id before numeric constant
   sf::Window window(sf::VideoMode(600,400), "Title");
                                   ^
test.cpp: In member function ‘void Control::run()’:
test.cpp:12:17: error: ‘((Control*)this)->Control::window’ does not have class type
    while (window.isOpen()){
                 ^
test.cpp:14:17: error: ‘((Control*)this)->Control::window’ does not have class type
     while(window.pollEvent(event)){
                 ^
test.cpp:16:13: error: ‘((Control*)this)->Control::window’ does not have class type
       window.close();
             ^
test.cpp:20:11: error: ‘((Control*)this)->Control::window’ does not have class type
     window.display();
           ^
test.cpp: In function ‘int main2()’:
test.cpp:53:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
Last edited on
You can't initialize stuff that way in classes. For that, you need a constructor (a default constructor in this case).

1
2
3
4
5
6
7
class classname {
    //private members
public:
    classname() {
        //Initialization code here.
    }
}


http://www.cplusplus.com/doc/tutorial/classes/ (scroll down a bit)

-Albatross
I initially did have that. I as well get this error when its in the constructor
1
2
3
4
5
6
7
8
9
10
11
test.cpp: In member function ‘void Control::run()’:
test.cpp:14:20: error: ‘window’ was not declared in this scope
             while (window.isOpen()){
                    ^
test.cpp:21:30: error: request for member ‘restart’ in ‘clock’, which is of non-class type ‘clock_t()throw () {aka long int()throw ()}’
                 time = clock.restart();
                              ^
test.cpp: In function ‘int main2()’:
test.cpp:55:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

class Control{
    public:
        Control(){
            sf::Window window(sf::VideoMode(600,400), "Title");
            sf::Time time;
            sf::Clock clock;
        }
        
        void run(){
            while (window.isOpen()){
                sf::Event event;
                while(window.pollEvent(event)){
                    if (event.type == sf::Event::Closed){
                        window.close();
                    }
                }
                time = clock.restart();
                window.display();
            }
        }
};

int main(){
    Control app;
    app.run();
}
Noooo, not quite.

You were right to have the declarations where you had them. It's just that initialization bit (the stuff that has to do your window object and everything in parantheses) needs to be taken care of in the constructor.

-Albatross
i didnt know that you had to initialize it in the constructor and declare only where i had it. However i am not sure why as doing the same with an int for example has no problem?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

class Klass{
    public:
        int var = 123;
        
};

int main(){
    Klass obj;
    std::cout << obj.var;
}


but yet you cannot do?

1
2
3
4
5
class Klass{
    public:
        sf::Window window(sf::VideoMode(600,400), "Title");
        
};


oh and for those googling, the final result:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

class Control{
    public:
        sf::Window window;
        sf::Time time;
        sf::Clock clock;
        
        Control(){
            window.create(sf::VideoMode(600,400), "Title");
        }
        
        void run(){
            while (window.isOpen()){
                sf::Event event;
                while(window.pollEvent(event)){
                    if (event.type == sf::Event::Closed){
                        window.close();
                    }
                }
                time = clock.restart();
                window.display();
            }
        }
};

Last edited on
Topic archived. No new replies allowed.