2014-04-19 62 views
0

爲什麼這樣的表達式不可接受?我想原型名爲子功能:processTheSelectedWord接受一個字符串,並沒有返回void函數需要一個字符串

void processTheSelectedWord (string); 

我得到的錯誤是:

1-非法使用類型爲void

2- '字符串'未聲明的標識符

這是代碼,如果您需要查看它

#include <iostream> 
    #include <iomanip> 
    #include <vector> 
    #include <fstream> 
    #include <string> 
    #include<time.h> 
    #include <conio.h> 
    #include <windows.h> 
    #include <wincon.h> 
    void processtheselectedword (string); 

    using namespace std; 

    void main() 
    { 
     srand(time(NULL)); 
     ifstream key; 
     key.open("Text.txt"); 

     string temp; 
     int counter = 0; 
     vector <string> wordslist; 
     while (!key.eof()) 
     { 
      key >> temp; 
      wordslist.push_back(temp); 
      counter ++; 
     } 

     int randomeWord = rand()% counter; 
     string word_to_guess = wordslist.at(randomeWord) ; 
     wordslist.erase(wordslist.begin() + randomeWord); 

     for (int i = 0; i < word_to_guess.length(); i++) 
      cout << "__"<< setw(4) ; 
     cout << endl; 
     cout << word_to_guess << endl; 
     key.close(); 
     processtheselectedword(word_to_guess); 
     system ("pause"); 

    } 

    //process the selected word 
    void processtheselectedword(string word_to_guess) 
    { 
    int trialsNum; //number of trials allowed based on the difficulty level 
    string dashes; 
    for (int i = 0; i < word_to_guess.length(); i++) 
     dashes.at(i) = '_'; 

    char guessedCharacter; 
    for (int j = 0; j < trialsNum; j++) 
    { 
     guessedCharacter = _getch(); 
     for (int i = 0; i < word_to_guess.length(); i++) 
      if (word_to_guess.at(i) == guessedCharacter) 
      { 
       dashes.replace(i,"guessedCharacter"); 
       cout << dashes; 
      } 
    } 

}提前

回答

2

感謝你似乎忘了,包括頭<string>並指定字符串聲明的命名空間。例如,你可以寫

#include <string> 

void processTheSelectedWord (std::string); 
2

你需要有一個定義,或者至少一個宣言,一個叫string可用的類型。想必您要的標準庫的std::string,在這種情況下,您需要#include <string>,並將其稱爲std::string

#include <string> 

void processTheSelectedWord (std::string); 
+0

解釋爲什麼不在有人開始詢問之前使用名稱空間可能很有用。 – lpapp

+0

@juanchopanza 我沒有包含字符串庫,但我沒有使用(標準::)我改爲使用命名空間,所以我不能看到問題 –

+0

@A_Mattar然後發佈一些真正的代碼。 – juanchopanza

相關問題