c++ - 错误 : "numeric_limits" was not declared in this scope

标签 c++ compiler-errors

我有这个程序无法为我编译,我不明白为什么,我收到错误“numeric_limits”未在此范围内声明,并且预期主表达式在“int”之前

这是我的代码:

#include <iostream>
#include<string>
#include<vector>
using namespace std;
using std::endl;
using std::vector;
using std::cout;


int main(){

    string lName;
    int yourAge;

    cout << "Please enter your last name: ";
    cin >> lName;
    cout << " Please enter your age: ";
    cin >> yourAge;
    cin.ignore(numeric_limits<int>::max(), '\n');
    if (!cin || cin.gcount() != 1){
        cout << "Invalid age, numbers only please \n";
        return yourAge;
    }

    vector<string> lastNames;
    lastNames.push_back("Jones");
    lastNames.push_back("Smith");
    lastNames.push_back("Whitney");
    lastNames.push_back("Wang");
    lastNames.push_back(lName);

    for(int i = 0; i < lastNames.size(); i++) {
        cout << "Names before removal: " << lastNames[i] << endl;
    }

    lastNames.erase(lastNames.begin()+2);
    for(int i = 0; i < lastNames.size(); i++) {
        cout << "Names after removal: " << lastNames[i] << endl;
    }
} 

最佳答案

您没有包含 std::numeric 所在的 header 。

#include <limits>

我建议不要使用 using namespace std;,而是使用您明确需要的所有名称:

using std::string;
using std::cin;
using std::endl;
using std::vector;
using std::cout;
using std::numeric_limits;

关于c++ - 错误 : "numeric_limits" was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20231259/

相关文章:

c++ - 如何重新启动我自己的 qt 应用程序?

java - 代号一个Java JRE版本

c++ - libstdc++ 对 std::unordered_map 的支持是否不完整?

c++ - 如何强制将常量 C 表达式求值用作 .S 文件中的常量?

c++ - 反转给定大小的链表时出现段错误

c++ - 如何对小数位进行分组?

c# - 无法创建泛型方法 : "T" not found

netbeans - Netbeans编译器错误位置错误

compiler-errors - 交叉编译 ARM 架构时出错

java - Java : Code too large for Oracle's javac is not so for Eclipse