0

I am writing code for reading a matrix from a file and deciding which type of relations it has. ie. Reflexive relation. However i am getting this error:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Binary' (or there is no acceptable conversion)

#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <istream>

using namespace std;

class Binary
{
private:
	bool** RelationMatrix;
	int Size;

void init()
{
	RelationMatrix = new bool*[Size];
	for (int i = 0; i < Size; i++)
		{
			RelationMatrix[i] = new bool[Size];
		}
}

public:
	Binary(int s)
		{
			Size = s;
			init();
		}

Binary& operator=(const Binary& t)
	{
		if (this == &t)
		{
			return *this;
		}
		else
		{
			Size = t.Size;
			for (int i = 0; i < Size; i++)
			{
				delete [] RelationMatrix[i];
			}
				delete [] RelationMatrix;
				init();
				for (int a = 0; a < Size; a++)
				{
					for (int b = 0; b < Size; b++)
					{
						RelationMatrix[a][b] = t[a][b];
					}
				}
		}
			return *this;
}

Binary(const Binary& binary)
	{
		Size = binary.gettingSize();
		init();
		*this = binary;
	}

~Binary()
	{
		for (int i = 0; i < Size; i++)
			{
				delete [] RelationMatrix[i];
			}
		delete [] RelationMatrix;
	}



int gettingSize() const
	{
		return Size;
	}

bool* operator[](int row) const
	{
		return RelationMatrix[row];
	}

bool operator==(const Binary& binary)
	{
		int s = binary.gettingSize();
		if (Size != s)
		{
			return false;
		}
			for (int i = 0; i < s; i++)
			{
				for (int j = 0; j < s; j++)
				{
					if (RelationMatrix[i][j] != binary[i][j])
					{
						return false;
					}
				}
			}
		return true;
	}

bool Binary::Reflexive()
	{
		for (int i = 0; i < Size; i++)
	{
		if (!RelationMatrix[i][i])
			return false;
	}
	return true;
}

bool Binary::Symmetric()
	{
		for (int a = 0; a < Size; a++)
		{
			for (int b = 0; b < Size; b++)
			{
				if (RelationMatrix[a][b] && !RelationMatrix[a][b])
					return false;
			}
		}
		return true;
	}

bool Binary::Antisymmetric()
	{
		for (int a = 0; a < Size; a++)
		{
			for (int b = 0; b < Size; b++)
			{
				if (RelationMatrix[a][a] && RelationMatrix[a][b] && (a != b))
					return false;
			}
		}
		return true;
	}

bool Binary::Transitive()
{
	for (int a = 0; a < Size; a++)
	{
		for(int b = 0; b < Size; b++)
		{
			if (RelationMatrix[a][b])
				{	
				for(int c = 0; c < Size; c++)
				{	
					if(RelationMatrix[b][c] && !RelationMatrix[a][c])
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}
void Binary::Result()
{
	cout << "\nThe matrix " << Size << "by" << Size << "\n" << *this << "is:\n";
	cout << "Reflexive: " << (Reflexive() ? "" : "NO ") << "Yes\n";
	cout << "Symmetric: " << (Symmetric() ? "" : "NO ") << "Yes\n";
	cout << "AntiSymmetric: " << (Antisymmetric() ? "" : "NO ") << "Yes\n";
	cout << "Transitive: " << (Transitive() ? "" : "NO ") << "Yes\n";
}

};
int main(int argc, char* argv[])
{
for (int i = 1; i < argc; i++)
{
string file = argv[i];
ifstream inFile(file.c_str());

if (inFile.is_open())
{
int s;
inFile >> s;
Binary binary(s);
inFile >> binary;
inFile.close();
binary.Result();
}
else
{
cout << "File does not work, sorry. ";
}
}

return 0;
}

I am not sure why i am getting this error. Any help would be greatly appreciated.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by gusano79
0

If you're going to use the << and >> streaming operators, you need to define their behavior--the compiler can't decide what you want them to mean.

For example:

void Result()
    {
        cout << "\nThe matrix " << Size << "by" << Size << "\n" << this << "is:\n";
        cout << "Reflexive: " << (Reflexive() ? "" : "NO ") << "Yes\n";
        cout << "Symmetric: " << (Symmetric() ? "" : "NO ") << "Yes\n";
        cout << "AntiSymmetric: " << (Antisymmetric() ? "" : "NO ") << "Yes\n";
        cout << "Transitive: " << (Transitive() ? "" : "NO ") << "Yes\n";
    }

    ostream& operator<< (ostream& out)
    {
        return out << Size; // Change this to output whatever you want
    }

Note that if you make the operator part of the class, you don't need to dereference this (line 3 in the example).

A similar operator definition is needed for >> that reads from an istream .

Also, what compiler are you using? GCC flags additional errors... you don't need to say something like bool Binary::Reflexive() if you're providing function bodies in the class definition; you can just use bool Reflexive()

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.