Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:38:40+00:00 2026-05-27T15:38:40+00:00

This is a rather simple question, and I think it breaks down to whether

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:38:41+00:00Added an answer on May 27, 2026 at 3:38 pm

    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()) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm rather new to WPF, so maybe this is a simple question. I have
I searched google but couldn't find an answer to this rather simple question. I
I will try to keep this as simple as possible. I have a rather
I think this is a pretty simple question but I'm having trouble finding the
I have a rather simple question for you.. I feel like I should have
I have a very simple (rather stupid) question, I hope someone can clear my
I think this question is probably fairly simple, but I've been searching around and
I think, the answer to my question is rather simple, but I just can't
Simple question I have a class say person who has as a property a
This is a very general question, and I think my context does not matter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.