Problem Statement:
I'm working through the Towers of Hanoi puzzle. In creating the Game class, I'm using an overloaded << operator to output the contents of the stacks. The compiler doesn't seem to be recognizing the overloaded operator at all, however, giving me the error "error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const value_type’ {aka ‘const std::stack<Cube>’})
os << "Stack[" << i << "]: " << game.stacks_[i];
Header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#ifndef GAME_H
#define GAME_H
#include"cube.h"
#include<stack>
#include<vector>
#include<ostream>
class Game
{
public:
Game();
void solve();
friend std::ostream& operator<< (std::ostream & os, const Game & game); //Problematic statement
private:
std::vector<std::stack<Cube>> stacks_;
};
#endif
|
Code segment in cpp file:
1 2 3 4 5 6 7 8
|
std::ostream& operator<< (std::ostream & os, const Game & game)
{
for (int i = 0; i < game.stacks_.size(); i++)
{
os << "Stack[" << i << "]: " << game.stacks_[i];
}
return os;
}
|
I've tried explicitly defining the implementation as a member of the Game class, which fixes the first problem, but introduces issues with implicit arguments, I've tried declaring it as a basic member function, which breaks even more stuff, and I;ve even gone so far as to cheat against a working copy of the code, which looks exactly like mine as far as I can tell. At this point, I'm stumped and could use some advice.
I have considered simply writing a print function that would output what I need, but from everything I can tell, this *should* work, and I would like to know why it isn't.
Followup question: What is the reason behind this function returning an ostream object?
Compiler: g++ Debian 8.2.0