首页 > 解决方案 > 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'.

标签: c++decltype

解决方案


decltype 仅在 C++11 中引入,推测 DevC++ 并没有指示编译器使用 c++11 模式。在您的编译器选项中,指示 DevC++ 传递以下命令行标志:

-std=c++11

推荐阅读