首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译练习时std_lib_facilities.h C++错误

编译练习时std_lib_facilities.h C++错误
EN

Stack Overflow用户
提问于 2016-01-10 03:04:56
回答 1查看 701关注 0票数 1

我试图在Bjarne的“编程原理和实践使用C++”一书中为一个练习编写一个程序,这个程序应该是一个名为“公牛和奶牛”的小游戏,用户试图猜出一个四位数的数字。这是我到目前为止掌握的代码:

代码语言:javascript
复制
#include "../Library/std_lib_facilities.h"

class string_error{};

/*
 * Method to determine if an integer is
 * in a vector
 */
bool contains(vector<int> v,int k ){

}

int string_to_int(string s){
    if(s == "0") return 0;
    else if(s == "1")return 1;
    else if(s == "2")return 2;
    else if(s == "3")return 3;
    else if(s == "4")return 4;
    else if(s == "5")return 5;
    else if(s == "6")return 6;
    else if(s == "7")return 7;
    else if(s == "8")return 8;
    else if(s == "9")return 9;
    else throw string_error();
}

int main(){

    /*
     * There is an int vector with the numbers
     * of the answer in it
     *
     * There is also an int vector with the numbers
     * of the user's guess
     *
     * User inputs a four digit integer that is read as
     * a string called str_guess
     *
     * for each digit in the guess string the digit is
     * converted to an integer and added to the guess vector
     *
     * The guess vector is then run through for each digit in the vector
     * each digit is checked against the answer at the same index
     * if the digit in the answer at that index number is not the same as the
     * digit in the guess vector then the digit is checked if the answer
     * vector contains it at all
     *
     * for each number that is correct for both the value and position
     * 1 is added to the bull value
     * for each number that is within the answer value but not in the correct position
     * 1 is added to the cow value
     *
     * The bull and cow values are displayed after the user enters their guess
     *
     * The user is prompted for a guess until their guess returns 4 bulls
     */

    vector<int> answer;
    answer.push_back(7);
    answer.push_back(9);
    answer.push_back(2);
    answer.push_back(4);

    string str_guess = "";
    cout << "Enter a four digit integer as a guess";
    cin >> str_guess;
    vector<int> guess;
    for(int i = 0; i < 4; i++){
        int guess_digit = string_to_int(str_guess[i]);
        guess.push_back(guess_digit);
    }
    cout << "Length of guess vector: " + guess.size();



}

正如您所看到的,我使用的是Mr.Stroustrup先生的头文件std_lib_facilities.h(这里),不幸的是,每当我试图编译我的程序时,我都会得到以下错误:

代码语言:javascript
复制
In file included from ../Bulls_and_Cows.cpp:13:
../../Library/std_lib_facilities.h:106:38: error: no matching constructor for initialization of 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
        template<class S> String(S s) :std::string(s) {}

我在mac上使用eclipse和mac os gcc编译器。如果你能为我提供任何帮助,我将不胜感激。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-10 03:38:49

函数string_to_int接收一个字符串,并传递一个char。

代码语言:javascript
复制
int guess_digit = string_to_int(str_guess[i]);
//                                       ^^^ access a position of the string that is a char
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34701589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档