简体   繁体   English

在头文件中声明字符串返回函数。 类型冲突?

[英]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: 在我的一个C文件中,功能是这样的:

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? 我不在乎中间部分,因为我现在不知道它是否重要,但是整个“'keyBoard'的冲突类型”废话又是什么呢? 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.5.2.2函数调用)

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. 6如果表示被调用函数的表达式的类型不包含原型,则对每个自变量执行整数提升,并将具有float类型的自变量提升为双精度。 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 . 但是在这种情况下,您必须考虑默认的参数提升

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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