I am writing a Bull Cow Game program and am getting a weird error in my header file.
The error is: "'initializing': cannot convert from 'void' to 'std::basic_string<char,std::char_traits<char>, std::allocator<char>>" is on line 9 and 10 of BullCowGame.cpp. What does this mean?
I am compiling using the Visual Studio 2017 compiler.
#include <string>
#include <algorithm>
#include "BullCowGame.h"
usingnamespace std;
void setNumOfCows(string guess, string answer)
{
string sortedGuess = sort(guess.begin(), guess.end());
string sortedAnswer = sort(answer.begin(), answer.end());
for (unsignedint i = 0; i < answer.size(); i++)
{
/*in order for the character at the current index of the Guess to be a Cow
it must be BOTH a character found somewhere in the Answer
AND NOT identical to the character at the same index of Answer
(i.e. if guess == "Marf" and answer == "Farm", "M" and "F" in the Guess
would be Cows since they are characters found in answer but are in the wrong
place; "A" and "R" would NOT be Cows even though they are characters found in
the Answer because they ALSO are identical characters at the same respective
index locations in Answer)*/
if ((guess[i] != answer[i]) && (sortedGuess[i] == sortedAnswer[i])
{
storedCows++;
}
}
}
int getNumOfCows()
{
return storedCows;
}
void setNumOfBulls(string guess, string answer)
{
for (unsignedint i = 0; i < answer.size(); i++)
{
if (guess[i] == answer[i])
{
storedBulls++;
}
}
}
int getNumOfBulls()
{
return storedBulls;
}
> The error is: "'initializing': cannot convert from 'void' to 'std::basic_string<char,std::char_traits<char>, std::allocator<char>>" is on line 9 and 10 of BullCowGame.cpp. What does this mean?
The error is: "'initializing': cannot convert from 'void' to 'std::basic_string<char,std::char_traits<char>, std::allocator<char>>" is on line 9 and 10 of BullCowGame.cpp. What does this mean?
std::string is actually a typedef for std::basic_string<char, std::allocator<char>> so what the error actually says is that it cannot convert from void to std::string.
So it seems like you can't assign the sorted string to a variable?
std::sort manipulates the sequence in-place. It does not produce a new string. After you have called sort(guess.begin(), guess.end()) the guess string will now be sorted.
I guess my question is: What doesn't sort() returning void mean? How can you return "void"?
What he means is that the return type is void, which means the function does not return any values.