- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 Bjarne Stroustrup 所著的 Programming Principles and Practice Using C++ 一书中的练习编写一个程序,该程序应该是一个名为 Bulls and Cows 的小游戏,用户可以在其中尝试猜一个四位数。这是我到目前为止的代码:
#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();
}
如您所见,不幸的是,每当我尝试编译我的程序时,我都会遇到此错误:
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 编译器。您能为我提供的任何帮助将不胜感激。谢谢
最佳答案
你的函数 string_to_int
接收一个字符串,你传递一个 char
int guess_digit = string_to_int(str_guess[i]);
// ^^^ access a position of the string that is a char
关于c++ - 编译练习时 std_lib_facilities.h C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34701589/
我正在使用Codeblocks 17.12,并且已经将编译器设置设置为C++ 11标准。我正在从Bjarne Stroustrup的书“编程-使用C++的原理和实践”中学习。在他的书中,他要求包括“s
我正在使用 Codeblocks 17.12,并且已经将编译器设置设置为 C++11 标准。我正在学习 Bjarne Stroustrup 的书“Programming - Principles an
我正在尝试在我的代码中使用 header std_lib_facilities,但我认为我做错了什么,因为当我尝试使用这段代码时: #include "../std_lib_facilities.h"
我正在通过 Bjarne Stroustrup 的《编程:原则与实践》学习 C++。他们给出了一个示例程序: // read and write a first name #include "std_
我正在尝试为 Bjarne Stroustrup 所著的 Programming Principles and Practice Using C++ 一书中的练习编写一个程序,该程序应该是一个名为 B
我是 C++ 的新手,我正在使用这本书学习 - Programming Principles & Practice using C++ by Bjarne Stroustrup,第 1 版。作者为每个
我正在学习 Bjarne Stroustrup 的书“Programming - Principles and Practice using C++”。在他的书中,他要求包括“std_lib_faci
我正在尝试让 Stroustrup 的“std_lib_facilities.h”头文件(位于此处:http://stroustrup.com/Programming/PPP2code/std_lib
我是一名优秀的程序员,十分优秀!