2013-02-26 152 views
-1

我不明白发生了什么事。我编了几次程序,一切都很顺利。但是由于我插入了#include <unordered_map>,我得到错误,例如cout上的未声明的标识符...没有用于getline的重载函数的实例。我正在使用Visual Studio 10. 另外,如果有人能告诉我如何正确初始化unordered_map,那就太好了。标识符cout未定义..和其他

#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; 
} 
+6

'using namespace std;'请停止这样做。 – 2013-02-26 13:33:46

+0

奇怪的部分是,只有第一cout给我的错误,但所有getlines显示为错误 – user1665569 2013-02-26 13:34:28

+0

我试着把它拿出来,并使用std,但我得到错误,当我试图调用std :: sha1 @nicolBolas – user1665569 2013-02-26 13:35:23

回答

1

看到这个post关于using namespace std,为什么不使用它。下面的代码仍然不能编译,但只有sha1的缺失定义有错误,您可能已经在某处。 (并且我在Mymap之上添加了结构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; 
} 
相关问题