我用C语言编写了一个程序,我想在此代码中使用C ++库,尽管我可以在g ++中编译C语言,因为C ++是在C语言的基础上构建的。但是,我无法做到这一点,并且存在主要错误因为在代码的一部分中,我编写了一个函数,该函数从main函数之前从输入文件中读取数据。在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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句