cannot convert from "void" to "std::basic_string"

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.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include "BullCowGame.h"

using namespace std;

void printIntro();
string getGuess();
void playGame();

int main()
{
	printIntro();

	playGame();
	
	return 0;
}


void printIntro()
{
	constexpr int WORD_LENGTH = 5;

	cout << "Welcome to Bulls and Cows, a fun word game.\n" << endl;
	cout << "Can you guess the " << WORD_LENGTH << " letter isogram I am thinking of?\n" << endl;
}

void playGame()
{
	constexpr int NUM_OF_TURNS = 10;
	string answer = "barn";

	for (int i = 0; i < NUM_OF_TURNS; i++)
	{
		BullCowGame bullCowGame;

		string guess = getGuess(); 

		bullCowGame.setNumOfCows(guess, answer);
		bullCowGame.setNumOfBulls(guess, answer);

		cout << "Number of Bulls: " << bullCowGame.getNumOfBulls()
 << "  Number of Cows: " << bullCowGame.getNumOfCows() << endl;
		cout << endl;

	}

}

string getGuess()
{
	string guess = "";

	cout << "Enter your guess: ";

	getline(cin, guess);
	cout << endl;

	return guess; 
}


BullCowGame.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef BULL_COW_GAME_H
#define BULL_COW_GAME_H

#include <string>

using namespace std; 

class BullCowGame
{
public:
	explicit BullCowGame(int initialBull = 0, int initialCow = 0)
		:storedBulls{ initialBull }, storedCows{ initialCow } {}
	
	void setNumOfCows(string guess, string answer);
	int getNumOfCows();
	void setNumOfBulls(string guess, string answer);
	int getNumOfBulls();

		
private:
	int storedBulls = 0;
	int storedCows = 0;
	
	
};

#endif	 


BullCowGame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <algorithm>
#include "BullCowGame.h"

using namespace std;

void setNumOfCows(string guess, string answer)
{
	string sortedGuess = sort(guess.begin(), guess.end());
	string sortedAnswer = sort(answer.begin(), answer.end());

	for (unsigned int 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 (unsigned int i = 0; i < answer.size(); i++)
	{
		if (guess[i] == answer[i])
		{
			storedBulls++;
		}
	}
}

int getNumOfBulls()
{
	return storedBulls;
}
Last edited on
> 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?

It means sort() returns void.
http://www.cplusplus.com/reference/algorithm/sort/

Sort sorts the thing 'in place'.
What thing 'in place'? The guess variable? Or something else?
So it seems like you can't assign the sorted string to a variable?

I changed lines 9 and 10 to this:

1
2
3
4
5
string sortedGuess = guess;
sort(sortedGuess.begin(), sortedGuess.end());
	
string sortedAnswer = answer;
sort(sortedAnswer.begin(), sortedAnswer.end());


and the compiler didn't throw an error.

I guess my question is: What doesn't sort() returning void mean? How can you return "void"?
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.
Last edited on
Topic archived. No new replies allowed.