Reputation: 69
I can't seem to figure out what I am doing wrong ... I am writing a program to play the game quatro, which is a board game kind of like tic tac tow. In the game their are 16 unique pieces in which each piece has four "traits". (Tall or Short , Black or White , Hollow or Filled, Square or Circle)
I created a structure "pieces" in which "traits" is a member. I refer to traits as W for white, T for Tall, H or hollow ....
Anyway, I am just trying to create an array which holds all available pieces and displays them in a separate scaled down program. However I can't seem to print out the elements of the available array.
the error i am getting is
error: not match for 'operator <<' in std:cout << available[0]'
Here is what I am trying now ...
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct Pieces{
string traits;
};
int main()
{
Pieces p1;
p1.traits = "WSHR";
Pieces p2;
p2.traits = "BSHR";
Pieces p3;
p3.traits = "WTHR";
Pieces p4;
p4.traits = "BTHR";
Pieces available [4] = {p1.traits,p2.traits,p3.traits,p4.traits};
cout << available[0];
return 0;
}
I added a bunch of library's hoping that was the problem. When I dereference the available array ( &available[0] ) the address prints out but I can't seem to figure out how to print the value in the first slot of the array.
Any help would be greatly appreciated.
Upvotes: 1
Views: 5596
Reputation: 1
error: not match for 'operator <<' in std:cout << available[0]'
You have to define a std::ostream& operator(std::ostream&, const Pieces&)
operator to print out Pieces
directly:
std::ostream& operator(std::ostream& os, const Pieces& pcs) {
os << pcs.traits;
return os;
}
Alternatively you can simply output the traits
member from the struct Pieces
directly:
std::cout << available[0].traits;
Upvotes: 1
Reputation: 1200
You should overload the '<<'operator (output stream operator), or output the 'traits' member. Also, you can use a custom constructor to make them easier to initialize, and even use the std::vector container:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Pieces{
Pieces(){} // default c'tor
Pieces(const string& t)
: traits(t) {}
string traits;
friend ostream& operator<< (ostream& stream, const Pieces& p);
};
// overload the output stream operator for convenience
ostream& operator<< (ostream& stream, const Pieces& p)
{
stream << p.traits;
return stream;
}
int main()
{
Pieces p1("WSHR"); // neater if you write a constructor overload that inits the contents
Pieces p2("BSHR");
Pieces p3("WTHR");
Pieces p4("BTHR");
Pieces available[4] = { p1, p2, p3, p4 };
// Or you could init the elements directly in the array;
Pieces a2[1] = { Pieces("WSHR") };
cout << available[0] << endl;
// This is my favorite way to declare and init the array
vector<Pieces> avail = { Pieces("WSHR"), Pieces("BSHR"), Pieces("WTHR"), Pieces("BTHR") };
cout << avail[0] << endl;
}
Upvotes: -1
Reputation: 42828
Because the array available
contains objects of type Pieces
, with cout << available[0]
you're trying to pass a Pieces
object to operator<<
of std::cout
.
But cout::operator<<
is not overloaded for the type Pieces
so it doesn't know what to do.
Either overload the operator<<
[1] or simply output the string inside that struct:
cout << available[0].traits;
[1] See πάντα ῥεῖ's answer for an example.
Upvotes: 1