A little class problem

This is a discussion on A little class problem within the C++ Programming forums, part of the General Programming Boards category; This is a class I'm using for a game I'm making and it is giving me these error; Code: C:\Users\Bijan\Documents\C++ ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Question A little class problem

    This is a class I'm using for a game I'm making and it is giving me these error;
    Code:
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|18|error: ISO C++ forbids initialization of member `x'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|18|error: making `x' static|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|18|error: ISO C++ forbids in-class initialization of non-const static member `x'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|19|error: ISO C++ forbids initialization of member `y'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|19|error: making `y' static|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|19|error: ISO C++ forbids in-class initialization of non-const static member `y'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|21|error: ISO C++ forbids initialization of member `right'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|21|error: making `right' static|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|21|error: ISO C++ forbids in-class initialization of non-const static member `right'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|22|error: ISO C++ forbids initialization of member `left'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|22|error: making `left' static|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|22|error: ISO C++ forbids in-class initialization of non-const static member `left'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|23|error: ISO C++ forbids initialization of member `up'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|23|error: making `up' static|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|23|error: ISO C++ forbids in-class initialization of non-const static member `up'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|24|error: ISO C++ forbids initialization of member `down'|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|24|error: making `down' static|
    C:\Users\Bijan\Documents\C++ Programs\My SDL project\main.cpp|24|error: ISO C++ forbids in-class initialization of non-const static member `down'|
    ||=== Build finished: 18 errors, 2 warnings ===|
    here is the class
    Code:
    class character{
        public:
        void handle_events();
    
        int x = 0;
        int y = 0;
    
        int right = 0;
        int left = 0;
        int up = 0;
        int down = 0;
    
        private:
        int collision_x();
        int collision_y();
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The errors mean just what they say. You cannot give member objects a value in a class definition, since quite frankly they don't exist. They do not exist until an object of that class is constructed, and so this initialization is the job of the constructor.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Those values will all be initialized to 0 by default anyway (when an actual object is instantiated). If you want to initialize them to something else, use an initialization list:
    Code:
    class test {
    	public:
    		int x, y;
    		test() : x(5), y(6) {}
    };
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    Those values will all be initialized to 0 by default anyway (when an actual object is instantiated).
    This is of course not true.
    Code:
    #include <iostream>
    class test {
        public:
            int x;
    };
    
    int main() {
        test foo;
        std::cout << foo.x << std::endl;
        return 0;
    }
    Code:
    $ ./a.out
    3616756

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Thanks you guys.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    This is of course not true.
    Um:
    Code:
    using namespace std;
    
    class test {
    	public:
    		int x, y;
    		test() {}
    };
    
    int main() {
    	test eg;
    
    	cout << eg.x << eg.y;
    
    	return 0;
    }
    00

    Maybe a constructor is required or it is just g++, but I'm sure I've read the standard ensures this too. Lemme go consult BS...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, Barney has nothing to say, but I am most likely wrong here, this is just a g++ thing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    It may be a g++ thing but also can be a thing where compiling in release mode does nothing for variables and compiling in debug mode more often than not means that they are initialized to zero. Older compilers used to init them to all kinds of crazy values (oxDEADBEEF anyone?) in debug more but the less colorful 0 is the norm now.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    Um:
    Code:
    using namespace std;
    
    class test {
    	public:
    		int x, y;
    		test() {}
    };
    
    int main() {
    	test eg;
    
    	cout << eg.x << eg.y;
    
    	return 0;
    }
    00

    Maybe a constructor is required or it is just g++, but I'm sure I've read the standard ensures this too. Lemme go consult BS...
    And what did you think I was using?
    Code:
    andrewf@ubuntu:~$ cat >mk.cpp
    #include <iostream>
    using namespace std;
    
    class test {
        public:
            int x, y;
            test() {}
    };
    
    int main() {
        test eg;
        
        cout << eg.x << eg.y;
    
        return 0;
    }
    andrewf@ubuntu:~$ g++ mk.cpp
    andrewf@ubuntu:~$ ./a.out
    134514368-1080144264

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    The tie-breaker (moi) goes to tabstop, though it does blow my debug mode theory. For:
    Code:
    class Foo
    {
    public:
       int x, y;
       Foo()
       {
          cout << "x,y in ctor: " << x << ", "<<  y << endl;
       }
    
    };
    in main.cpp, compiled on:
    Code:
    jeff@jeff-gate:~/dev/test1$ gcc --version
    gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
    Copyright (C) 2009 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    jeff@jeff-gate:~/dev/test1$
    With switches:
    Code:
     -Wall -ggdb
    And called from main() like so:
    Code:
    int main(int argc, char *argv[])
    {
       cout << "foo_test 1.0" << endl;
       Foo foo;
       cout << "Foo::x and Foo::y in main: " << foo.x << foo.y << endl;
    Produces:
    Code:
    jeff@jeff-gate:~/dev/test1$ make clean && make
    [ 50%] Building CXX object CMakeFiles/foo.dir/foo.cpp.o
    Linking CXX shared library libfoo.so
    [ 50%] Built target foo
    [100%] Building CXX object CMakeFiles/foo_test.dir/foo_main.cpp.o
    Linking CXX executable foo_test
    [100%] Built target foo_test
    jeff@jeff-gate:~/dev/test1$ ./foo_test 
    foo_test 1.0
    x,y in ctor: 134524916, -1079089064
    Foo::x and Foo::y in main: 134524916-1079089064
    Hmmph.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Update:
    Compiling with -O2 produced the additional compile output as well as the now more interesting program output:
    Code:
    jeff@jeff-gate:~/dev/test1$ make clean && make
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/jeff/dev/test1
    [ 50%] Building CXX object CMakeFiles/foo.dir/foo.cpp.o
    Linking CXX shared library libfoo.so
    [ 50%] Built target foo
    [100%] Building CXX object CMakeFiles/foo_test.dir/foo_main.cpp.o
    /home/jeff/dev/test1/foo_main.cpp: In function ‘int main(int, char**)’:
    /home/jeff/dev/test1/foo_main.cpp:21: warning: ‘foo.Foo::x’ may be used uninitialized in this function
    /home/jeff/dev/test1/foo_main.cpp:36: note: ‘foo.Foo::x’ was declared here
    /home/jeff/dev/test1/foo_main.cpp:21: warning: ‘foo.Foo::y’ may be used uninitialized in this function
    /home/jeff/dev/test1/foo_main.cpp:36: note: ‘foo.Foo::y’ was declared here
    Linking CXX executable foo_test
    [100%] Built target foo_test
    jeff@jeff-gate:~/dev/test1$ ./foo_test 
    foo_test 1.0
    x,y in ctor: 0, 0
    Foo::x and Foo::y in main: 00
    Testing: foo
    jeff@jeff-gate:~/dev/test1$
    So there you have it.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I wasn't using any switches, gcc 4.3.2.

    I guess the point is that I shouldn't count on that (time to go thru some code! ARGGGHH!). I could have sworn I read this was a standard thing somewhere.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    I wasn't using any switches, gcc 4.3.2.

    I guess the point is that I shouldn't count on that (time to go thru some code! ARGGGHH!). I could have sworn I read this was a standard thing somewhere.
    It's supposed to default-construct each member of the class. But default-construct for int doesn't do a great deal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21