简体   繁体   中英

Declaring a string returning function in a header file. Conflicting Types?

I have this in my header file:

const char * keyBoard();

And in one of my C files the function is this:

const char * keyboard() 
{
     //mycode
     return string;
}

I get this error with my compiler:

 error: conflicting types for 'keyBoard'  
 const char * keyBoard(char hintTxt[30], int maxNumbers, bool multiLine)  
....................^~~~~~~~  
note: an argument type that has a default promotion can't match an empty parameter name list declaration  
 {  
 ^  
(in header file) note: previous declaration of 'keyBoard' was here  
 const char * keyBoard();  
....................^~~~~~~~

I don't care about the middle part because I don't know if it matters right now, but what is with the whole "conflicting types for 'keyBoard'" crap? They are the exact same as far as I can tell, and I can't find any help on this topic

According to the error message, your function is defined as:

const char * keyBoard(char hintTxt[30], int maxNumbers, bool multiLine)

But you declare it as:

const char * keyBoard();

These don't match. The declaration has to match the definition:

const char * keyBoard(char hintTxt[30], int maxNumbers, bool multiLine);

You declared the function without its prototype that is the number and types of parameters are unknown

const char * keyBoard();

In this case when the function is called the compiler performs the so-called default argument promotions . And the error message means the following (6.5.2.2 Function calls)

6 If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions ....

You should declare the function specifying its parameters before to call it.

const char * keyBoard(char hintTxt[30], int maxNumbers, bool multiLine);

Take into account that a declaration does not need to match the function definition. You may declare a function without its parameters. But in this case you have to take into account the default argument promotions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM