c++ - GCC 无法编译 : '* does not name a type'

标签 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

最佳答案

三个错误:

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

  2. 您必须输入 using namespace std;在文件中,或引用 std::coutstd::cin明确地。随你选吧,很多 C++ 程序员对于这两个选项中哪个更好是意见不一的。 (如果需要,您也可以只将 cincout 带入您的命名空间。)

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

这里是更正:

#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/

相关文章:

linux - 无法在 Linux 上安装 perl 模块

c - sched_setaffinity 和 glibc - 交叉编译

c++ - 数组指针的增量

c++ - 使用私有(private)析构函数删除动态分配的对象

c - 在编译器之间匹配 va_list 类型

c++ - ProtoBuf 编译链接 GCC

c++ - 为什么我不能创建一个包含 vec3 对象的 union ?

c++ - 为什么std :set (with a single colon) compile?可以

c++ - 如何在 C++ 中将无符号长数组转换为字节?

gcc - gcc 将如何对同一命令中的 -O2 和 -O0 使用react?