c++ - decltype 未在此范围内声明
问题描述
我正在尝试制作一个读取字符串并告诉我其中有多少标点符号的程序。但是,当我尝试编译它时,它给出了错误,“'decltype' 未在此范围内声明'。我上个月刚开始使用 c++ 并且对它的概念很陌生。
我使用 Dev C++ 5.11 作为代码的 IDE。代码来自书 c++ Primer 第五版第 92 页
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main() {
string s("Hello World!!!");
decltype(s.size() punct_cnt = 0;
// count the number of punctuation characters in s
for (auto c : s) // for every char in s
if (ispunct(c)) // if the character is punctuation
++punct_cnt;
cout << punct_cnt << " punctuation characters in " << s << endl;
}
I expect it to give the output 3, but it gives the error message, "'decltype' was not declared in this scope'.
解决方案
decltype 仅在 C++11 中引入,推测 DevC++ 并没有指示编译器使用 c++11 模式。在您的编译器选项中,指示 DevC++ 传递以下命令行标志:
-std=c++11
推荐阅读
- python-3.x - 如何从给定登录日期之前过去发生的 timedelta 值中获取确切日期?
- r - 如何删除观星者中的引文?
- mongodb - 通过 Annotation 在 Spring Data 中使用默认语言为 mongodb 定义通配符文本索引
- python - 将文本中的元素添加到字典
- mysql - 在 3 个表中使用 join 查询 knex
- javascript - 为什么将 SVG 图像导入到 Icon 对象时不显示?
- swift - 选择更改时,通过 NSViewRepresentable 将值从 NSTextView 传递到父 SwiftUI 视图
- excel - 如何在不同的 Excel 工作表中运行宏?它没有循环
- asp.net-core - 使用 EF .net 5 在 ASP.NET Core 中使用迷你探查器出现“未找到”错误
- algorithm - 计算 a[i] 最右边或最左边且最大的段