gpt4 book ai didi

以 C 风格编写的 C++ 程序无法使用 C 编译器编译

转载 作者:行者123 更新时间:2023-11-30 20:50:43 26 4
gpt4 key购买 nike

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

#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'

<小时/>可能是什么问题呢?我的重载出了什么问题?

最佳答案

I have a working C++ code written in C style, that has an oveloaded function [..]

停下来!

C 确实支持函数重载,因此会出现编译错误。阅读更多 Does C support overloading?

关于以 C 风格编写的 C++ 程序无法使用 C 编译器编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40695108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com