DECLTYPE 未在此范围内声明

decltype was not declared in this scope

本文关键字:范围内 声明 DECLTYPE      更新时间:2023-10-16

我正在尝试制作一个读取字符串并告诉我其中有多少标点符号的程序。但是,当我尝试编译它时,它会给出 错误"'decltype'未在此范围内声明"。我刚刚开始 上个月的C ++,并且对其概念很陌生。

我正在使用 Dev C++ 5.11 作为代码的 IDE。代码来自书中 c++ 入门第五版,第 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