Thread: I cannot get one simple class to work

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    I cannot get one simple class to work

    Hi!

    I'm learning C++ programming. I got a problem with simple class.

    Here is the code:
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Menu
    {
       int NUMMENUS = 0;
       char* labels [NUMMENUS];
       bool selected[NUMMENUS];
    
       public:
       void Setnum (int i) {
          NUMMENUS = i;
       };
    
       int Getnum()
       {
          return NUMMENUS;
       };
    };
    
    int main(void)
    {
       Menu menu;
    
       menu.Setnum(8);
    
       cout<<"Number on: "<<menu.Getnum()<<"\n";
       return 0;
    }

    And here is the error:
    Code:
    leho@leho-laptop:~/Dokumendid/OpenGL/Menüü$ g++ teine.cpp -o teine
    teine.cpp:8:18: error: invalid use of non-static data member ‘Menu::NUMMENUS’
        char* labels [NUMMENUS];
                      ^~~~~~~~
    teine.cpp:7:17: note: declared here
      int NUMMENUS = 0;
                     ^
    teine.cpp:9:18: error: invalid use of non-static data member ‘Menu::NUMMENUS’
        bool selected[NUMMENUS];
                      ^~~~~~~~
    teine.cpp:7:17: note: declared here
      int NUMMENUS = 0;
                     ^
    Where is the error?

    Leho

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char* labels [NUMMENUS];
    > bool selected[NUMMENUS];
    Array sizes are compile time constants in C++.
    You're trying to make it variable.

    Perhaps you should be looking at say std::vector, which is something you can resize.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > int NUMMENUS = 0;

    You can't initialise variables like that inside a class. Only const static qualified variables can.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Zeus_ View Post
    > int NUMMENUS = 0;

    You can't initialise variables like that inside a class. Only const static qualified variables can.
    You can do that: it sets the default member initialiser, although the initialisation itself is only done when the constructor is invoked, i.e., when the initialiser list would be run.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Then my GCC is outdated because I sure do get an error if I try to write code like that.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For older versions of GCC, you might have to explicitly compile as C++11 with -std=c++11

    Well, at least I think this is a C++11 feature; if it was introduced in C++14 then you might be stuck, and likewise if your version of GCC does not support C++11 (but if so, you should upgrade as that version of GCC is arguably obsolete).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2019
    Posts
    214
    To expand on Laserlight's point, one of the primary reasons to use GCC is that it is often first to implement current/new C++ features, so keeping one's GCC toolset current is usually quite important, though sometimes the most recent versions may be more difficult to install on some platforms. These days, on Windows with Visual Studio, Clang is much easier to use than GCC, while on Linux it is often easier to keep GCC current than it is for Clang.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  2. Simple C++ Won't Work...
    By jothesmo in forum C++ Programming
    Replies: 10
    Last Post: 11-25-2005, 07:00 PM
  3. Simple class doesn't work
    By Cris987 in forum C++ Programming
    Replies: 7
    Last Post: 01-08-2004, 11:18 PM
  4. Why won't this class work?
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2002, 06:46 PM

Tags for this Thread