Artie
Artie

Reputation: 503

Using the overloaded operators ">" and "<" from the vector container in the C++ STL (Not dereferencable)

This is a rather simple question, and I think it breaks down to whether or not this is legal. I have a class with the ">" and "<" operators overloaded. I'm trying to make a single statement that will return true or false without having to declare an iterator for the class, just for "simplicity".

Here is an example of what I'm talking about:

class Guy
{
  double bigness; //I'm not illiterate by the way

public:
  bool operator>(const Guy& that) {return (this->bigness > that.bigness);}
}; 

Ok, this right there should work. Now I make two vectors of type guy, and I want to compare two members in a single expression so I get the truth value for that expression, like this:

vector<Guy> gang, mafia;
bool mafiaWins = (*(mafia.begin()) > *(gang.begin()));

I didn't populate the vector, so I wouldn't get anything reasonable from this code. But how could I manage to have the expression (mafia.begin() > gang.begin()) to work? I know they are both returning iterators; but dereferencing them doesn't work. Using .front() gives me the same error. I get an assertion for having a non-dereferencable iterator (Shouldn't it be dereferenciable?). I'm wondering if the problem is on not storing it in an iterator first...

Thanks for your attention

Solution:

So, the code I have above (Edited from when I first posted), should work for the expression. That is not the code I actually had in my .cpp, that's why its so messy. You don't need to use random access reference if you are only trying to reach the first member of the vector, and ,front() will the the reference for the first term. I originally left the vector empty in this post, because I thought it was irrelevant to populate them, and by doing that I accidentally got what my problem was in my code. The function I had getting vectors and checking their members was not checking whether or not the members I was comparing existed. So, for anyone that gets a similar assertion problem, you are possibly trying to dereference something that doesn't exist. For checking if a vector is empty there is a member function that can be used.

Example:
bool thereIsWar = (!(mafia.empty() && !(gang.empty()));
if(thereIsWar)
  bool mafiaWins = (*(mafia.begin()) > *(gang.being()));

Hope it helps anyone with a similar question

Upvotes: 0

Views: 144

Answers (2)

hmjd
hmjd

Reputation: 121961

that is a reference.

Change:

bool operator>(const Guy& that) {return (this->bigness > that->bigness);}

To:

bool operator>(const Guy& that) {return (this->bigness > that.bigness);}

Ensure your vector has at least one element before testing:

vector<Guy> gang, mafia;

gang.push_back(Guy());  // Not sure you how you set 'bigness'.
mafia.push_back(Guy());

Dereference iterators for comparision:

bool mafiaWins = (*mafia.begin() > *gang.begin()) ;

Upvotes: 1

111111
111111

Reputation: 16148

This should work so long as the vector has contents (I get a segfault if they are empty).

bool mafiaWins = (*mafia.begin()) > (*gang.begin());

as should

bool mafiaWins = mafia.front() > gang.front();

If it doesn't fix please post the exact error.

NB: the less than/greater than operator are defined for random access iterator for comparing position.

Upvotes: 1

Related Questions