标识符cout未定义......等等

时间:2013-02-26 13:31:13

标签: c++ cout getline unordered-map

我不明白发生了什么。我编写了几次程序,一切都很顺利。但是因为我插入了#include <unordered_map>,我得到的错误就是“cout上未声明的标识符......没有getline的重载函数实例”。我正在使用Visual Studio 10。 此外,如果有人可以告诉我如何正确初始化unordered_map,那就太棒了。

#include "stdafx.h"
#include<string>
#include <iostream>
#include <sstream>
#include <unordered_map>

using namespace std;

unordered_map<string, dictionary * > Mymap;

int _tmain(int argc, _TCHAR* argv[])
{
    string option;
    string pass;
    int choice=0;

    unsigned char hash[20];
    char hex_str[41];

    while(choice!=4)
    {
        cout<< "Select an option:"<< endl;
        cout<<"1. Basic Hashing"<<endl;
        cout<<"2. Load Dictionary"<<endl;
        cout<<"3. Decrypt"<<endl;
        cout<<"4. Exit" <<endl;

        getline(cin,option);
        stringstream(option) >> choice;

        if(choice == 1)
        {
            cout<<"Please enter a sample password"<<endl;
            getline(cin,pass);
            const char * c= pass.c_str();
            sha1::calc(c,pass.length(), hash);
            sha1::toHexString(hash,hex_str);
            cout<<endl;
            cout<<"Hashed: "<< hex_str<<endl;
        }
        else if(choice ==2)
        {
            string answer;
            cout<<"Would you like to use the default dictionary file(d8.txt). Press y or n"<<endl;
            getline(cin,answer);
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

有关using namespace std以及为何不使用它,请参阅此post。下面的代码仍然无法编译,但只有关于sha1缺少定义的错误,您可能在某处。 (我在Mymap之上添加了struct def,以减少错误。)

关于错误,通常C ++编译器会在它遇到的第一个错误描述中给你一个有意义的错误描述,但此后,事情可能会变得奇怪,所以你要一次修复它们以开始明白。

#include "stdafx.h"
#include <string>
#include <iostream>
#include <sstream>
#include <unordered_map>

typedef struct dictionary{ std::string word; char * hash; char *hex; } a_dictionary;
std::unordered_map<std::string, a_dictionary * > Mymap;

int _tmain(int argc, _TCHAR* argv[])
{
    std::string option;
    std::string pass;
    int choice=0;

    unsigned char hash[20];
    char hex_str[41];

    while(choice!=4)
    {
        std::cout<< "Select an option:"<< std::endl;
        std::cout<<"1. Basic Hashing"<<std::endl;
        std::cout<<"2. Load Dictionary"<<std::endl;
        std::cout<<"3. Decrypt"<<std::endl;
        std::cout<<"4. Exit" <<std::endl;

        getline(std::cin,option);
        std::stringstream(option) >> choice;

        if(choice == 1)
        {
            std::cout<<"Please enter a sample password"<<std::endl;
            getline(std::cin,pass);
            const char * c= pass.c_str();
            sha1::calc(c,pass.length(), hash);
            sha1::toHexstd::string(hash,hex_str);
            std::cout<<std::endl;
            std::cout<<"Hashed: "<< hex_str<<std::endl;
        }
        else if(choice ==2)
        {
            std::string answer;
            std::cout<<"Would you like to use the default dictionary file(d8.txt). Press y or n"<<std::endl;
            getline(std::cin,answer);
        }
    }
    return 0;
}