用C风格编写的C ++程序不能用C编译器编译

时间:2016-11-19 16:15:43

标签: c++ c function overloading

我是一名学生,开始学习一些以前的C知识的C ++。我有一个用C风格编写的工作C ++代码,它有一个oveloaded函数,用于输入doublestring

#include "stdio.h"
#include "string.h"
#include "ctype.h"

const int STR_LEN = 7;
int try_to_input(double* real_number);
int try_to_input(char (*string)[STR_LEN]);//line 16

int main()
{
    return 0;
}

int try_to_input(double* real_number) {//line 35
    int attempts_for_input = 3;
    //stops when == 0
    while (attempts_for_input) {
        //if input unsuccessful
        if (scanf_s("%lf", real_number) == 0) {
            puts("Invalid input! Try again.");
            attempts_for_input--;
            //flush stdin
            int tmp;
            while ((tmp = getchar()) != EOF && tmp != '\n');
            //extra '\n' after that for some reason
        }
        else return 0;
    }
    return -1;
}

int try_to_input(char (*string) [STR_LEN] ) { //line 53
    int attempts_for_input = 3;

    while (attempts_for_input) {
        if (gets_s(*string, STR_LEN) == NULL) {
            puts("Invalid input! Try again.\n");
            attempts_for_input--;
            //flush stdin
            int tmp;
            while ((tmp = getchar()) != EOF && tmp != '\n');
        }
        else return 0;
    }
    return -1;

}

但是当我把它编译为C时,它会给我以下错误:

(16): warning C4028: formal parameter 1 different from declaration
(35): warning C4028: formal parameter 1 different from declaration
(53): error C2084: function 'int try_to_input(double *)' already has a body
(35): note: see previous definition of 'try_to_input'

<小时/> 可能是什么问题呢?我的超载有什么问题?

2 个答案:

答案 0 :(得分:3)

  

我有一个用C风格编写的工作C ++代码,它有一个开发的函数[..]

在那里停下来!

C 支持函数重载,因此编译错误。阅读Does C support overloading?

中的更多内容

答案 1 :(得分:0)

正如Jonathon Reinhart评论的那样,C语言根本不支持重载函数