void函数接受一个字符串

时间:2014-04-19 13:35:31

标签: c++

为什么这样的表达是不可接受的?我正在尝试原型化一个名为:processTheSelectedWord的子函数,它接受一个字符串并且不返回任何内容

    void processTheSelectedWord (string);

我得到的错误是:

  

1-非法使用类型void

     

2-' string'未声明的标识符

这是代码,如果您需要查看它

    #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 个答案:

答案 0 :(得分:2)

您似乎忘记包含标头<string>并指定声明字符串的命名空间。例如,你可以写

#include <string>

void processTheSelectedWord ( std::string );

答案 1 :(得分:2)

您需要有一个名为string的类型的定义或至少一个声明。大概你想要标准库std::string,在这种情况下你需要#include <string>,并称之为std::string

#include <string>

void processTheSelectedWord (std::string);