SFML and STL: What's wrong with my code? - c++

I have the following code snippets:
void RemoveButton::triggerAction(team &team, unsigned int index)
{
switch (mAction)
{
case Action::remove:
{
//team.mTeamMembers.erase(std::remove(team.mTeamMembers.begin(), team.mTeamMembers.end(), team.mTeamMembers.at(index)), team.mTeamMembers.end());
team.mTeamMembers.erase(team.mTeamMembers.begin() + index);
for (unsigned int i = index; i < team.mTeamMembers.size(); i++)
{
team.mTeamMembers[i].mRemoveButton->getText().move(0.0f, -30.0f);
team.mTeamMembers[i].mText.move(0.0f, -30.0f);
}
team.mAddPosition.y -= 30.0f;
break;
}
default:break;
}
}
class team
{
public:
size_t teamNumber;
std::vector<AddButton> mAddButtons;
std::vector<teamRecord> mTeamMembers;
sf::Sprite mBorder;
sf::Text mText;
Selector<AddButton> mAddButtons_Selector;
sf::Vector2f mAddPosition;
Selector<teamRecord> mTeamMembers_Selector;
team(sf::Vector2f borderPosition, sf::Vector2f removeButtonsPosition, sf::Vector2f textPosition, size_t teamNumb, std::string text);
};
How should I erase an element from the team.mTeamMembers STL vector? The way I do it currently results in seemingly random execution errors, specifically when clicking the non-existent sprite of a deleted button. The commented line that uses std::remove is not compiled, and I do not understand why. As I'm feeling confused a.f. , can someone please shine some light here? Here's the build log while using std::remove, as recommended by some users on this forum:
1>------ Build started: Project: Complex OOP Menus, Configuration: Release Win32 ------
1> RemoveButton.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1454): error C2678: binary '==': no operator found which takes a left-hand operand of type 'teamRecord' (or there is no acceptable conversion)
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(389): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(381): note: or 'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(373): note: or 'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(365): note: or 'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\exception(339): note: or 'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) throw()'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\exception(334): note: or 'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) throw()'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\exception(329): note: or 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) throw()'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1454): note: while trying to match the argument list '(teamRecord, const teamRecord)'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1467): note: see reference to function template instantiation '_FwdIt std::_Remove_unchecked<teamRecord*,_Ty>(_FwdIt,_FwdIt,const _Ty &)' being compiled
1> with
1> [
1> _FwdIt=teamRecord *,
1> _Ty=teamRecord
1> ]
1> ..\Data\Source\RemoveButton.cpp(24): note: see reference to function template instantiation '_FwdIt std::remove<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<teamRecord>>>,teamRecord>(_FwdIt,_FwdIt,const _Ty &)' being compiled
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<teamRecord>>>,
1> _Ty=teamRecord
1> ]
If there is not enough information here, I could post the github link to my project, but it's pretty big and it would take time to sift through the code.

Using std::remove() doesn't make any sense to me if you already know the position/offset and you only want to remove a single entry.
You're receiving your error with std::remove() most likely due to the fact that there's no equality operator (operator ==) defined for your teamRecord class (or it just isn't found).
The way I do it currently results in seemingly random execution errors, specifically when clicking the non-existent sprite of a deleted button.
That's something we can't look at unless we see the code regarding that. How can you click something not existing? Any chance you're just missing a check or not cleaning up things properly?

I solved this by double checking the index I was giving to the elements on insertion. Thanks and sorry for such a stupid mistake!

Related

Different iterator behavior of raw array and std::array on clang++ and VC++

Consider following program:
#include <iostream>
#include <algorithm>
#include <array>
bool greater_than_seven(int i) {
return i > 5;
}
bool divisible_by_five(int x) {
return ((x%5)==0);
}
int main() {
int arr[]{3,6,9,12,15};
std::cout<<"Enter a number you want to search: ";
int num;
std::cin>>num;
auto result(std::find(std::begin(arr),std::end(arr),num));
if(result != std::end(arr))
std::cout<<"arr contains: "<<num<<'\n';
else
std::cout<<"arr doesn't contain: "<<num<<'\n';
for(result=std::find_if(std::begin(arr),std::end(arr),greater_than_seven);result!=std::end(arr);++result)
std::cout<<*result<<' ';
std::cout<<'\n';
std::array<int,4> x{33,66,99,55};
for(result=std::find_if_not(std::begin(x),std::end(x),divisible_by_five);result!=std::end(x);++result)
std::cout<<*result<<'\n';
}
This program compiles fine on g++ & clang++.
See live demo here ( g++ 5.4.0 )
See live demo here ( clang++ 3.8.0 )
But it gives horrible compiler error on Microsoft Visual C++ compiler.
See live demo here ( Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 )
Error(s):
source_file.cpp(27): error C2440: '=': cannot convert from 'std::_Array_iterator<_Ty,4>' to 'int *'
with
[
_Ty=int
]
source_file.cpp(27): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
source_file.cpp(27): error C2679: binary '!=': no operator found which takes a right-hand operand of type 'std::_Array_iterator<_Ty,4>' (or there is no acceptable conversion)
with
[
_Ty=int
]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(343): note: could be 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(348): note: or 'bool std::operator !=(std::nullptr_t,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(353): note: or 'bool std::operator !=(const std::exception_ptr &,std::nullptr_t) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(388): note: or 'bool std::operator !=(const std::error_code &,const std::error_code &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(395): note: or 'bool std::operator !=(const std::error_code &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(402): note: or 'bool std::operator !=(const std::error_condition &,const std::error_code &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(409): note: or 'bool std::operator !=(const std::error_condition &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
source_file.cpp(27): note: while trying to match the argument list '(int *, std::_Array_iterator<_Ty,4>)'
with
[
_Ty=int
]
So the question is which compiler is right here according to C++ standard ? Is this bug in VC++ compiler ?
Is this bug in VC++ compiler ?
No.
You're assigning and comparing the iterators got from std::begin and std::end on raw array (i.e. result, std::find(std::begin(arr),std::end(arr),num)) and std::array (i.e. std::find_if_not(std::begin(x),std::end(x),divisible_by_five) and std::end(x)), you might be supposing that the types of them are the same.
For raw arrays it'll be T*, i.e. int* for this case, this is guaranteed. The problem is that the standard doesn't specify the exact type of std::array::iterator, it just says it must satisfy the requirements of RandomAccessIterator. Gcc and Clang choose the int* as its type, this is fine because the raw pointer satisfy the requirements. VC implements it as a customized class, this is fine too so long as the type satisfies the requirements. And note that that type doesn't have to be able to convert to int*; the standard doesn't require that at all.
So even your code works with Gcc and Clang, it's not guaranteed.

What does the istream extraction operator >> return?

I'm trying to get Solipsis to compile in Visual Studio 2017(it was written for VS 2005)
I can't figure out what this code is trying to do:
template<typename T>
bool from_string( const char* Str, T & Dest )
{
// créer un flux à partir de la chaîne donnée
std::istringstream iss( Str );
// tenter la conversion vers Dest
return iss >> Dest != 0;
}
It gets the following error
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): error C2678: binary '!=': no operator found which takes a left-hand operand of type 'std::basic_istream<char,std::char_traits<char>>' (or there is no acceptable conversion)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\exception(347): note: could be 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\exception(352): note: or 'bool std::operator !=(std::nullptr_t,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\exception(357): note: or 'bool std::operator !=(const std::exception_ptr &,std::nullptr_t) throw()' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(379): note: or 'bool std::operator !=(const std::error_code &,const std::error_code &) noexcept' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(384): note: or 'bool std::operator !=(const std::error_code &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(389): note: or 'bool std::operator !=(const std::error_condition &,const std::error_code &) noexcept' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(394): note: or 'bool std::operator !=(const std::error_condition &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): note: or 'built-in C++ operator!=(bool, int)'
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): note: while trying to match the argument list '(std::basic_istream<char,std::char_traits<char>>, int)'
1>src\Object3D.cpp(220): note: see reference to function template instantiation 'bool Solipsis::from_string<bool>(const char *,T &)' being compiled
1> with
1> [
1> T=bool
1> ]
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): error C2446: '!=': no conversion from 'int' to 'std::basic_istream<char,std::char_traits<char>>'
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): note: Constructor for class 'std::basic_istream<char,std::char_traits<char>>' is declared 'explicit'
1>SolipsisErrorHandler.cpp
In human language what is the return value of the '>>' operator(when used as for extraction not bit shift)? What has changed about it since VS 2005 that makes the code snippet not work?
I can't figure out what this code is trying to do:
The code is trying to return whether the stream extraction was successful or not.
What does the istream extraction operator >> return?
It is not the case that the return type of the operator is breaking your compilation, but rather because of a change in behaviour since C++11.
Prior to C++11 (say with VS2005) you could check for success/failure by comparing the istream object with true/false.
return iss >> Dest != 0;
You can't do that with a C++11 compilation (say with VS2017), and the reasons are given in the excellent answer to the suggested duplicate question:
Evaluating stream operator >> as boolean
Rather modernise your function by casting to a boolean.
return bool(iss >> Dest);

sorting vector of structs by integer [duplicate]

This question already has answers here:
problem sorting using member function as comparator
(9 answers)
Closed 7 years ago.
Im trying to sort vector of structs. I saw this and this examples.
this is my code:
.h file:
// I didn't mention all the includes and namespace
class fileLoader
{
struct commands
{
int time;
string name;
};
commands resultStruct;
vector<commands> resultVector
private:
void sortFunction();
bool compareByTime(const commands &a, const commands &b);
}
.cpp file:
void fileLoader::sortResults()
{
sort(resultVector.begin(), resultVector.end(), compareByTime);
}
bool fileLoader::compareByTime(const commands &a, const commands &b)
{
return a.time < b.time;
}
this is the compilation error I get:
error C3867: 'fileLoader::compareByTime': function call missing argument list; use '&fileLoader::compareByTime' to create a pointer to member
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3639) : see declaration of 'std::sort'
when I tried to change compareByTime to &fileLoader::compareByTime, I got this compilation error:
error C2064: term does not evaluate to a function taking 2 arguments
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3776) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Unguarded_partition<_RanIt,_Pr>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Ty1=fileLoader::commands *,
1> _Ty2=fileLoader::commands *,
1> _RanIt=fileLoader::commands *,
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3806) : see reference to function template instantiation 'void std::_Sort<fileLoader::commands*,__w64 int,_Pr>(_RanIt,_RanIt,_Diff,_Pr)' being compiled
1> with
1> [
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &),
1> _RanIt=fileLoader::commands *,
1> _Diff=__w64 int
1> ]
std::sort<std::_Vector_iterator<_Myvec>,bool(__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Myvec=std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>,
1> _RanIt=std::_Vector_iterator<std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>>,
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3720): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.
I will be happy for some help.
thanks.
First fix all the typos (missing semicolons etc).
Then change your sorting function to static bool compareByTime(const commands &a, const commands &b);
Short answer
Make compareByTime static (or 'global' outside class)
Explanation
Member functions require this to be passed to them somehow, so two argument member function is probably 3 argument function. So compiler can't use it when it needs 2 arguments comparator.

What's wrong with map?

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <iterator>
using namespace std;
int main()
{
map<char*, int> m;
int N;
scanf ("%d", &N);
for (int i = 0; i < N; i++)
{
char name[256], s[256];
scanf ("%s", &name);
gets (s);
m[name]++;
}
map<char*, int>::iterator itr;
for (itr = m.begin(); itr != m.end(); itr++)
printf ("%s %d", itr->first, (itr)->second);
}
This program meant to read number of lines then lines, and then output the number of times each word has been repeated in start of lines
for example with this input:
3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna
I expect :
England 1
Spain 2
but I receive :
Spain 3
and I'm sure the the variable name is what it should be each time but something is wrong with the map.
my build log when I changed it to std::string
1>------ Build started: Project: UVa, Configuration: Debug Win32 ------
1> UVa.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(179) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(194) : see reference to function template instantiation 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const' being compiled
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(1028) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::less<std::string>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(67) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false>
1> ]
1> c:\users\amiraz\documents\visual studio 2012\projects\uva\uva\uva.cpp(13) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
char* are compared as pointers, not as strings. Use std::string instead.
And as P0W mentioned,scanf, printf, gets are C-style. Maps and iterators are C++-style. Don't mix them. Use std::cin, std::cout, std::getline for IO in C++.
If you map with a char* as a key, you're really mapping the with the address of the string, not the string itself as a key. Since your buffer is identically allocated on the stack every time, it will most likely end up at the same address and map to the same value, no matter the content.
Mapping from std::string, the C++ alternative to C strings, will instead automatically use the content of the string as a key.

error C2678: binary '<' : no operator found which takes a left-hand operand... (or there is no acceptable conversion) [duplicate]

This question already has answers here:
How can I use std::maps with user-defined types as key?
(8 answers)
Closed 5 years ago.
Here is my code to to find values in a map:
bool myclass::getFreqFromCache( plVariablesConjunction& varABC, vector<plFloat>& freq )
{
std::map<plVariablesConjunction, std::vector<plFloat>>::iterator freqItr;
freqItr = freqCache.find(varABC);
if (freqItr != freqCache.end())
{
freq = freqItr->second;
return true;
}
}
"PlVariablesConjunction" is a ProBT library datatype. it contains operator "==", if two variables found same then it returns true otherwise false.
Here is error:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const plVariablesConjunction' (or there is no acceptable conversion)
1> E:\ProBT22\probt-spl-2.2.0-expires-20121130-vc10-dynamic-release\include\plSymbol.h(71): could be 'bool operator <(const plSymbol &,const plSymbol &)' [found using argument-dependent lookup]
1> while trying to match the argument list '(const plVariablesConjunction, const plVariablesConjunction)'
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=plVariablesConjunction
1> ]
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1> with
1> [
1> _Ty=plVariablesConjunction
1> ]
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1> with
1> [
1> _Kty=plVariablesConjunction,
1> _Ty=std::vector<plProbValue>,
1> _Pr=std::less<plVariablesConjunction>,
1> _Alloc=std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,
1> _Mfl=false
1> ]
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(520) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1> ]
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(659) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1> ]
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1> ]
1> e:\probt22\work\yasin\testmmhcfinalversion\testmmhc_mi_probt_sw\mmhc\slidingWindow.h(55) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=plVariablesConjunction,
1> _Ty=std::vector<plProbValue>
1> ]
std::map is (usually) implemented as binary search tree, most often red-black tree. It needs linear order to be defined for key values to find correct position within a tree. That's why std::map tries to call operator< on inserted key values.
Your class doesn't provide operator<. Either define operator< for your class or provide comparison function for template: std::map<plVariablesConjunction, std::vector<plFloat>, my_comparison_function>.
To use the map class, require two, and possibly three, types for the template:
std::map <key_type, data_type, [comparison_function]>
Either you need to provide a comparison function or overload the < operator in the key class.
Notice that the comparison function is in brackets, indicating that it is optional so long as your key_type has the less-than operator, <, defined
map<> doesn't use operator== to check the inserted values. It needs a comparision via operator< for the key values.