GCC无法编译:'*没有命名类型'

时间:2012-06-15 04:53:06

标签: c++ gcc

今天,在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

2 个答案:

答案 0 :(得分:5)

三个错误:

  1. 正确的标头是<iostream>。该程序不需要其他标题。

  2. 您必须将using namespace std;放入文件中,或明确提及std::coutstd::cin。顺便提一下,很多C ++程序员不同意这两个选项中哪一个更好。 (如果需要,您也可以将cincout带入您的命名空间。)

  3. 程序最后没有写一个行终止符。这将导致输出在大多数终端上“看起来很糟糕”,命令提示符出现在与输出相同的行上。例如:

  4. 以下是更正:

    #include <iostream>
    using namespace std;
    int main(int argc, char *argv[])
    {
        ...
        cout << nFahrenheit << '\n';
        ...
    }
    

    注意:看到main使用argcargv以外的名称参数,这是极不寻常的。更改名称只会让其他人更难阅读您的代码。

答案 1 :(得分:1)

它的std :: cout或你应该添加using namespace std;

且包含应为< iostream>而不是< ionstream.h>