I have an enum class "Suit" and defined a function "string to_string(Suit e)"
In another class, "Card", I have a member variable "my_Suit" and a member-function "to_string". This function calls the function "to_string" with "my_Suit" as a parameter.
At compilation I get an error that the compiler (g++) is looking for the function "Card::to_string(Suit&)", but this function doesn't exist (not within the scope mentioned, "Card::").
The error states :
error: no matching function for call to ‘Card::to_string(Suit&)’
candidate: std::__cxx11::string Card::to_string()
How do I make clear to the compiler that he must search for the function defined outside the class ?
This is a piece of code that gives the error at compilation. In reality, the code is divided between several header- and source-files, but the error stays the same.
#include <iostream>
/******************** enum class Suit ********************/
enum class Suit
{
Clubs, Spades, Hearts, Diamonds
};
std::string to_string(Suit e)
{
return ("calling 'to_string' function with Suit as parameter");
}
/******************** clas Card ********************/
class Card
{
private:
Suit m_Suit;
public:
Card() { m_Suit = Suit::Clubs; }
std::string to_string()
{
return ( to_string(m_Suit) );
}
};
int main()
{
std::cout << "Hello world!\n";
return (0);
}