Unknown override specifier

Started by
2 comments, last by ukdeveloper 16 years, 6 months ago
Hi guys, My C++ is really rusty and I'm trying to get back into it again. I'm just doing simple console based stuff for now and I've hit an error I've never seen before. "Unknown override specifier". Here's my code:

#include <iostream>
#include <cstdlib>
using namespace std;

char firstName[]="";
char surname[]="";
int firstNameTotal = 0;
int surnameTotal = 0;

void showAscii(char[] input);


int main (int argc, char *argv[])
{

	cout<<"Please enter your first name: ";
	cin>>firstName;

	cout<<"Your first name is: "<<firstName<<"\n";
	showAscii(firstName);
	/*cout<<"Please enter your surname: ";
	cin>>surname;

	cout<<"Your surname is: "<<surname<<"\n";*/

	
	return (0);

}


// void showAscii(char[] input)
// 
void showAscii(char[] input)
{

	cout<<"DEBUG: The first character at position [0] is: "<<input[0]<<" and its ASCII value is: "<<int(input[0])<<".\n";

}



Here are the errors:

1>g:\ee21001\assignment one\one\one.cpp(19) : error C2146: syntax error : missing ')' before identifier 'input'
1>g:\ee21001\assignment one\one\one.cpp(19) : error C3646: 'input' : unknown override specifier
1>g:\ee21001\assignment one\one\one.cpp(19) : error C2059: syntax error : ')'
1>g:\ee21001\assignment one\one\one.cpp(42) : error C2146: syntax error : missing ')' before identifier 'input'
1>g:\ee21001\assignment one\one\one.cpp(42) : error C3646: 'input' : unknown override specifier
1>g:\ee21001\assignment one\one\one.cpp(42) : error C2059: syntax error : ')'
1>g:\ee21001\assignment one\one\one.cpp(45) : error C2065: 'input' : undeclared identifier
1>g:\ee21001\assignment one\one\one.cpp(45) : fatal error C1903: unable to recover from previous error(s); stopping compilation



I don't understand what unknown override specifier means and Google isn't helping me much. I suspect it's a problem with passing char[] input into the function but I don't see the problem. Line 42 refers to the function header void showAscii(char[] input), and line 19 refers to the declaration of this function at the top of my code. I've got a nasty feeling my problem is screamingly obvious but I really can't see it. Can anyone point me in the right direction? Thanks, ukd.
Advertisement
void showAscii(char input[])


Note the position of the []'s.
Even with the fix the program is still broken. If it doesn't crash it may appear to work, because you only print the first character of the name. But you might change showAscii to print it for the whole string, take both firstName and surname and then call the function on those.

char firstName[]="";char surname[]="";


Here you declare two character arrays. You are letting the compiler to deduce the size of the array from the provided string. Since it is empty, the size of both arrays will be 1 character - just enough to store the null-terminator. You can't legally put any more characters in it.

You'll need to declare larger arrays if you want to store anything in it:
char firstName[20];char surname[20];


Of course, the problem won't go away if the user's name happens to be longer than 20 (19) characters. For that reason C++ has a standard string class which takes care of aquiring and managing needed memory automatically and relieves you of most troubles.

Here's your program rewritten to use string:
#include <iostream>#include <string>using namespace std;//don't use global variables unless you really need to//char firstName[]="";//char surname[]="";//int firstNameTotal = 0;//int surnameTotal = 0;void showAscii(const string& input);int main (int argc, char *argv[]){    string firstName;    cout<<"Please enter your first name: ";    cin>>firstName;    cout<<"Your first name is: "<<firstName<<"\n";    showAscii(firstName);    return (0);}void showAscii(const string& input){        cout<<"DEBUG: The first character at position [0] is: "<<input[0]<<" and its ASCII value is: "<<int(input[0])<<".\n";}
The debug statement displaying input[0] was just to test that the ASCII values were being returned and displayed correctly, my code is updated and now works.

Thanks for your help.

This topic is closed to new replies.

Advertisement