今天,在安装 Slackware 13.37 之后,我遇到了问题:默认的 GCC 4.5.2 无法编译我的代码。现在我通过 Stephen Davis 的书“C++ for dummies”学习 C++ 并想编译它:
#include <stdio.h>
#include <iostream.h>
int main(int nNumberofArgs, char* pszArgs[])
{
int nNCelsius;
cout << "Celsisus: ";
cin >> nNCelsius;
int nNFactor;
nNFactor = 212 - 32;
int nFahrenheit;
nFahrenheit = nNFactor * nNCelsius / 100 + 32;
cout << "Fahrenheit: ";
cout << nFahrenheit;
return 0;
}
但是我的 GCC 4.5.2 给出了这些错误:
FahTCel.cpp:7:14: error: expected ')' before ';' token
FahTCel.cpp:7:14: error: 'main' declared as function returning a function
FahTCel.cpp:8:1: error: 'cout' does not name a type
FahTCel.cpp:9:1: error: 'cin' does not name a type
FahTCel.cpp:12:1: error: 'nNFactor' does not name a type
FahTCel.cpp:15:1: error: 'nFahrenheit' does not name a type
FahTCel.cpp:17:1: error: 'cout' does not name a type
FahTCel.cpp:18:1: error: 'cout' does not name a type
FahTCel.cpp:20:1: error: expected unqualified-id before 'return'
FahTCel.cpp:21:1: error: expected declaration before '}' token
最佳答案
三个错误:
正确的 header 是
<iostream>
.该程序不需要其他 header 。您必须输入
using namespace std;
在文件中,或引用std::cout
和std::cin
明确地。随你选吧,很多 C++ 程序员对于这两个选项中哪个更好是意见不一的。 (如果需要,您也可以只将cin
和cout
带入您的命名空间。)程序没有在末尾写行终止符。这将导致输出在大多数终端上“看起来很糟糕”,命令提示符与输出出现在同一行。例如:
这里是更正:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
...
cout << nFahrenheit << '\n';
...
}
注意:看到main
是极其不寻常的采用名称不是 argc
的参数和 argv
.更改名称只会让其他人更难阅读您的代码。
关于c++ - GCC 无法编译 : '* does not name a type' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044774/