错误:未在此范围内声明“numeric_limits”

时间:2013-11-27 01:22:17

标签: 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;
    }
} 

1 个答案:

答案 0 :(得分:7)

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

#include <limits>

我建议使用明确需要的所有名称代替using namespace std;

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