我用 C 编写了一个程序,我想在这段代码中使用 C++ 库,我想我将能够在 g++ 中编译 C,因为 C++ 内置于 C 之上。但是,我不能这样做,并且主要错误是因为在代码的一部分中,我在主函数之前编写了一个从输入文件读取数据的函数。这在 C 编译器中运行良好,但在 Cpp 编译器中运行不佳。 以下是我收到的一些错误消息,因此我想获得一般性评论以及互换使用 c 和 cpp 时要考虑的要点
error : ‘get_inputs’ was not declared in this scope
error: use of parameter outside function body before ‘]’ token
最佳答案
以下程序用 C 语言编译,并带有警告,例如:'bar' undefined;假设 extern 返回 int
void foo()
{
bar(5);
}
int bar(int x)
{
return x*2;
}
如果您希望在 C++ 中编译它,则必须在使用它之前声明 bar
:
int bar(int x); // forward declaration
void foo()
{
bar(5);
}
int bar(int x)
{
return x*2;
}
即使在 C 语言中,使用前向声明并启用所有编译器警告也是一个很好的做法,否则以下程序中的错误将被忽略:
void foo()
{
bar(); // calling bar without argument....
}
int bar(int x)
{
return x*2; // ... will result in an undefined value for x here
}
关于c++ - 在C++编译器中编译C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35621588/