简体   繁体   English

c 和 c++ function 声明和定义:指定数据类型

[英]c and c++ function declaration & definition : specifying data type

I am working on porting c code to c++.我正在将 c 代码移植到 c++。 The below C program compiles successfully.下面的 C 程序编译成功。 But in c++, it throws an error.但是在 c++ 中,它会引发错误。 I know its really basic thing.我知道它非常基本的东西。 I've more than 100 functions declared in this way.我以这种方式声明了 100 多个函数。 Is there any flag we can add to compile successfully instead of modifying all the functions.是否可以添加任何标志来成功编译,而不是修改所有函数。

  • OS: Windows操作系统:Windows
  • IDE: Visual Studio IDE:Visual Studio
int sum(m,n)
int m,n;
{
    return m+n;
}

This is very old style C.这是非常老式的 C。 Nobody should have been using this for the last 30 years.在过去的 30 年里,没有人应该使用它。 You can't compile this with a C++ compiler.你不能用 C++ 编译器编译它。 There are no compiler flags whatsoever for this, at least in the compilers I'm aware of (clang, gcc, microsoft compilers).对此没有任何编译器标志,至少在我知道的编译器中(clang,gcc,微软编译器)。

You should transform all functions that have the form您应该转换所有具有以下形式的函数

int sum(m,n)
int m,n;
{
    return m+n;
}

int to the form int 到表格

int sum(int m, int n)
{
    return m+n;
}

If you have 100 functions it should be doable.如果你有 100 个函数,它应该是可行的。 But there may be many other problems because C++ is not an exact superset of C.但可能还有许多其他问题,因为 C++ 不是 C 的精确超集。

This SO article may be interesting for you: Function declaration: K&R vs ANSI这篇 SO 文章可能会让您感兴趣: Function 声明:K&R vs ANSI

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

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