2013-02-26 148 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; 
} 
相關問題