What compiler, and compiler version are you using? It looks like you're using an older version of whatever compiler you happen to be using. Most of the current compilers are using c++14 by default, older compilers often used c++98 by default.
Have you tried adding the -std flag to your build line (either -std=c++11, -std=c++14, -std=c++17)?
I've enabled c++11 for my compiler and IDE but am now getting this strange error message. Anyone know what the problem is?:
||=== Build: Debug in Object Testing (compiler: GNU GCC Compiler) ===|
|1820|required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*>
|1873|required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key,
|610|required from 'std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>,
|119|required from here|
|387|error: no match for 'operator<' (operand types are 'const Nurse' and 'const Nurse')|
(x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\stl_pair.h|220|note: candidate: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)|
Thanks for the advice there, I've updated to GCC 9.2 the 64 bit version. However, the same error message is still being generated.
I think you are correct in that I need to implement the operator function with const qualifiers - but what would this implementation and function signature look like?
a std::map uses a binary search tree (bst) to allow for fast insert/delete/find operations on the set of keys
to be able to construct a bst, the elements stored must be comparable (less, greater than)
1 2 3 4 5 6 7 8 9 10
class Nurse{
//...
public:
booloperator<(const Nurse &b) const; //declaration
};
bool Nurse::operator<(const Nurse &b) const{ //definition
//true if *this is less than b, false otherwise
//¿how do you compare nurses?
}
Overloading the operator< function is giving me some trouble here. What arguments should it take? What does it specifically need to do?
Regarding declaring the global variables "name" and "id" in the Nurse.cpp file - I thought that this was an appropriate way to declare the Nurse class's private variables (where the name and id data for each nurse could be stored and then modified / accessed by the implemented getter and setter functions).
Is there a more appropriate way of doing this? How could an instantiation of a class store data if that class didn't have private variables?
What arguments should it take? What does it specifically need to do?
Look as some of the previous posts, they have shown what arguments are required. And it need to "compare" the class to an "external" class (the parameter). Since you're overloading the less than operator you need to decide what makes this class less than the "external" class.
Regarding declaring the global variables "name" and "id" in the Nurse.cpp file - I thought that this was an appropriate way to declare the Nurse class's private variables (where the name and id data for each nurse could be stored and then modified / accessed by the implemented getter and setter functions).
What you're actually doing is declaring two variables totally unrelated to your class, that under certain circumstances will conflict with each other.
You've already declared two variables in the class declaration so these global variables are not needed.
Thank you all. I'm comparing Nurse objects based on their ID, so I also had to declare and implement the getID() function as a const as well (so I could compare *this id with the external Nurse id in the operator function).
I'll have to look more into C++ OOP to understand the correct way to not have those variables exist in the class and be able to have the class without declaring them as global variables.
I'll have to look more into C++ OOP to understand the correct way to not have those variables exist in the class and be able to have the class without declaring them as global variables.
What? Why wouldn't you want have those variables in the class?
Thank you all. I'm comparing Nurse objects based on their ID, so I also had to declare and implement the getID() function as a const as well (so I could compare *this id with the external Nurse id in the operator function).
Well all of your "getters" should probably be declared const, since a getter should not modify the variable. However you should be able to just compare the two ids from the two classes.
Something similar to (where name is a private data member) :
1 2 3 4
booloperator<(const Nurse& other) const
{
return name < other.name;
}
Perhaps I am misunderstanding your previous comment:
What you're actually doing is declaring two variables totally unrelated to your class, that under certain circumstances will conflict with each other.
You've already declared two variables in the class declaration so these global variables are not needed.
Maybe you are referring to the "name" and "id" variables on the cpp file, yes? Yes, I would like to have these variables in the class but perhaps you are suggesting that I am declaring them twice, or declaring two different "name" and "id" variables altogether (once in the header file and again in the cpp file)?
Is the "name" and "id" declaration in the cpp file incorrect?
If so, then would the setters/getters be able to access the name and id variables on the header file, even though those variables are not declared in the cpp file itself?
Maybe you are referring to the "name" and "id" variables on the cpp file, yes?
Well those are the global variables I'm talking about yes.
Yes, I would like to have these variables in the class
You do have them in the class, see lines 11 and 12 of your code.
but perhaps you are suggesting that I am declaring them twice,
No, you are declaring two different variables named name and id in two different scopes. Once inside your class (the ones you want to keep) and once in the .cpp file (the global variables you want to get rid of).
Is the "name" and "id" declaration in the cpp file incorrect?
Yes, they are incorrect.
If so, then would the setters/getters be able to access the name and id variables on the header file
All class member functions can access all the member variables of the class no matter where the member functions are defined.
Thank you very much!! I see now, this makes sense - thank you for clearing this up. I'm more familiar with writing classes in Java so that could explain my mix up here.
The bottom line - if the key class is complex, unlike my previous, a purpose designed comparator is required. (I'd be surprised if Java isn't the same.)