c++ - 无法使用 gcc 编译 C++

标签 c++ gcc

我需要在我的 linux 系统上编译代码。这是简单的代码,我不知道出了什么问题:

我有这段代码,但无法编译:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string char1, char2, char3, char4, char5, char6;
    cout<<"Hello this is your standard True and False quiz"<<endl;
    cout<<"Please enter 'T' or 'F'"<<endl;
    cout<<"No#1 George Washington invented the toilet?"<<endl;
    cin>>char1;
    if ( char1 != "T" || "F")
    {
        cout<<"You entered an incorrect character please reenter True of False"<<endl;
        cin>>char1;
    }
    if ( char1 != "T" || "F")
    {
        cout<<"You entered an incorrect character please reenter True of False"<<endl;
        cin>>char1;
    }
    if ( char1 == "T" )
    {
        cout<<"You entered the incorrect answer. The answer is False"<<endl;
    }
    cout<<"No#2 The Squareroot of 3136 is 56?"<<endl;      
    cin>>char2;
    if ( char2 != "T" || "F")
    {
        cout<<"You entered an incorrect character please reenter True of False"<<endl;
        cin>>char2;
    }
    if ( char2 != "T" || "F")
    {
        cout<<"You entered an incorrect character please reenter True of False"<<endl;
        cin>>char2;
    }
    if ( char2 == "F" )
    {
        cout<<"You entered the incorrect answer. The answer is True"<<endl;
    }
    cout<<"No#3 
    system("PAUSE");
    return 0;
} 

当我尝试编译它时:

gcc file.c

我明白了:

test.c:1: fatal error: iostream: No such file or directory
compilation terminated.

据我所知,我拥有所有需要的库,我做错了什么?

最佳答案

您正在尝试使用 C 编译器编译 C++。试试 g++ file.c而是。

另外,最好将文件命名为 file.cpp相反 - 将其命名为 .c不会停止它的编译,但它会帮助像 make 这样的工具.此外,它将帮助以后遇到您的源代码的其他人(包括您自己)。

编辑:您的代码还有一些与您的问题无关的其他问题,但您会在编译后立即遇到它们:

  1. 您的( char1 != "T" || "F")应该是 ( char1 != "T" && char1 != "F") (注意 && 而不是 || )
  2. 当您输入错误时,您正在读取另一个字符,但由于您的代码没有循环,因此程序将在对下一个字符执行任何操作之前退出。
  3. 正如 David Schwartz 指出的那样,it's worth removing system("PAUSE")

对于 C 语言的新手来说,这些是很常见的错误(欢迎!我建议从一些教程或介绍性书籍开始。这里有一本优秀的 list of C books and tutorials)。

如果您遇到无法自行解决的问题,请随时提出另一个问题。

关于c++ - 无法使用 gcc 编译 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8597975/

相关文章:

c++ - C++ 中不同 channel 的 LUT,opencv2

c++ - 使用混合版本的 gcc 的 Makefile

使用 exec() 编译和运行 c 程序

perl - agl 构建在 "recipe perl-native-5.24.4-r0: task do_configure:"中失败

C++ 复制/移动构造函数和赋值运算符

c++ - 为什么我不能通过推回将值存储在我的 2D vector 中?

c++ - CMake - 包含项目外部的目录

c++ - gcc c++ 命令行错误消息解析器

gcc - 是否可以关闭 glibc 端的 vdso?

c++ - 用非常量变量声明数组大小