Why won't this compile?

This is a discussion on Why won't this compile? within the C++ Programming forums, part of the General Programming Boards category; Why won't this compile? Im using Visual C++ 6 and the error I receive has something to do with the ...

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    Why won't this compile?

    Why won't this compile? Im using Visual C++ 6 and the error I receive has something to do with the way I declared the character array 'info' within the public section of class 'help' along with several others about the member functions and invalid overloading and all this other gibberish. thanx in advance

    Code:
    #include <iostream>
    using namespace std;
    class help {
    public:
    	char info[][50]={
    		"",
    		"if(condition) statement;",
    		"switch(expression) { //... };",
    		"for(init;condition;iteration) { //...};",
    		"while(condition) { //...};",
    		"do{ //...} while(condition);",
    		"break;",
    		"continue;",
    		"goto label;"
    	};
    	void helpon(int query);
    	void showmenu();
    	bool isvalid(int query);
    };
    void help::helpon(int query) {
    	cout<<info[query]<<endl;
    	return;
    }
    void help::showmenu() {
    	cout<<"Help on:"<<endl;
    	cout<<"1: if"<<endl<<"2: switch"<<endl<<"3: for"<<endl<<"4: while"<<endl<<"5: do while"<<endl<<"6: break"<<endl<<"7: continue"<<endl<<"8: goto"<<endl<<"9: functions"<<endl<<"0: quit"<<endl;
    }
    void help::isvalid(int query) {
    	if(query<0||query>9) return false;
    	else return true;
    }
    int main() {
    	int query;
    	help cpp;
    	for( ; ; ) {
    		do {
    			cpp.showmenu();
    			cin>>query;
    		} while(cpp.isvalid(query));
    		if(!query) break;
    		cpp.helpon(query);
    	}
    	return 0;
    }

    Code Tags Added By Hammer

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I'm not sure, because I didn't look at the whole code, but I think you might be having a problem in this:
    Code:
    char info[][50]={
    "",
    "if(condition) statement;",
    "switch(expression) { //... };",
    "for(init;condition;iteration) { //...};",
    "while(condition) { //...};",
    "do{ //...} while(condition);",
    "break;",
    "continue;",
    "goto label;"
    };
    Try using an array of char pointers( char *info[size]; ), and I think it will do the job.
    none...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    An array of pointers to char is generally better than a two dimensional array. The problem was that you were trying to initialize the array inside the class definition, this doesn't make sense to the compiler so you get errors. Try this instead:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class help {
    public:
      static char *info[];
      
      void helpon(int query);
      void showmenu();
      bool isvalid(int query);
    };
    
    char *help::info[]={
      "",
      "if(condition) statement;",
      "switch(expression) { //... };",
      "for(init;condition;iteration) { //...};",
      "while(condition) { //...};",
      "do{ //...} while(condition);",
      "break;",
      "continue;",
      "goto label;"
    };
    
    void help::helpon(int query) {
      cout<<info[query]<<endl;
      return;
    }
    
    void help::showmenu() {
      cout<<"Help on:"<<endl;
      cout<<"1: if"<<endl<<"2: switch"<<endl<<"3: for"<<endl<<"4: while"<<
        endl<<"5: do while"<<endl<<"6: break"<<endl<<"7: continue"<<endl<<
        "8: goto"<<endl<<"9: functions"<<endl<<"0: quit"<<endl;
    }
    
    bool help::isvalid(int query) {
      if(query<0||query>9)
        return false;
      else
        return true;
    }
    
    int main() {
      int query;
      help cpp;
      for( ; ; ) {
        do {
          cpp.showmenu();
          cin>>query;
        } while(!cpp.isvalid(query));
        if(!query)
          break;
        cpp.helpon(query);
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The only problem with using arrays of pointers to chars and initializing them in the way that Prelude recommends, is that you must make sure you never try to alter the data of the string whose first element the pointer is pointing to (was that english?). You should treat it as though it was pointing to "const" data (and in this case it appears as though you might want to declare it as such). If you ever wish to edit the data being pointed to you should dynamically allocate the strings or make them point to global or static arrays.
    Last edited by Polymorphic OOP; 12-29-2002 at 05:30 PM.

  5. #5
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    wow.. so many quality replies in such little time. thanks alot. I understand that i need to put the static identifier before the array and define it outside of the class. thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 01:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 07:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 08:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:58 PM

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