-
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
-
I'm not sure, because I didn't look at the whole code, but I think you might be having a problem in this:
Quote:
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.
-
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
-
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.
-
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