编译c++错误

Compile C++ error

本文关键字:错误 c++ 编译      更新时间:2023-10-16
#include "std_lib_facilities.h"
int main()
{
constexpr double euro_to_dollar = 1.11;
constexpr double yen_to_dollar = 0.0081;
double dollar = 1.00;
char unit= 'A';
cout <"Please enter a value followed by e or y: n";
cin >>dollar>> unit;
if (unit= 'e')
cout << dollar << "Euro == " << euro_to_dollar*dollar << "dollarn";
else if (unit='y')
        cout << dollar << "Yen== " << yen_to_dollar * dollar << "dollarn";
}
<>之前没有在此范围内声明'constexpr'5 .错误:期望';'在'double'之前7 .错误:期望';'在'double'之前错误:'euro_to_dollar'未在此范围内声明在此范围内没有声明'yen_to_dollar'之前

我正在做一个问题从编程:原理和实践使用c++(第二版)由Bjarne Stroustrup。我可以看到我做错了什么。我是一个尝试学习c++的人,所以我基本上是个初学者。我很感谢你们的帮助。

我不知道你的本地头文件中有什么

#include "std_lib_facilities.h"

我添加了以下代码行

#include<iostream>
using namespace std;

if (unit = 'e')//正在做赋值应修改如下

if (unit == 'e'),//检查是否相等

由于同样的原因,else if (unit ='y')应该更正如下

else if (unit =='y')

此外,您应该使用编译器选项-std=c++11

进行编译。

在c++ 11中引入了constexpr关键字,用-std=c++11编译。


的例子:

with g++: g++ main.cpp -o program.exe -std=c++11

with code::blocks:
Settings -> Compiler -> Compiler Settings -> Compiler Flags ->选中Have g++ follow the C++ ISO C++ language standard -> Ok


你也分配给一个变量在你的if语句,替换为==:

if (unit= 'e')
    //  ^

else if (unit='y')
        //   ^

在调用std::cout:

时缺少<
cout <"Please enter a value followed by e or y: n";
 //  ^