This question already has answers here:
Sorting Sets using std::sort
(3 answers)
Closed 2 years ago.
I have this code:
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cctype>
#include <string>
...
void f() {
std::set<std::pair<int, std::string>> orderByRating;
/*...*/
auto revIter = orderByRating.rbegin();
int subsetSz = 2;
auto cmp = [](std::pair<int, std::string> const & a, std::pair<int, std::string> const & b) {
return a.second < b.second;
};
std::sort(revIter, std::next(revIter, subsetSz), cmp);
}
And I get this error
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2679: binary '-': no operator found which takes a right-hand operand of type 'const std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=std::pair<int,std::string>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(1383): note: could be 'std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>> std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>::operator -(int) const'
1> with
1> [
1> _Ty=std::pair<int,std::string>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): note: while trying to match the argument list '(const std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>, const std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>)'
1> with
1> [
1> _Ty=std::pair<int,std::string>
1> ]
1>c:\users\user\source\repos\consoleapplication1\consoleapplication1.cpp(66): note: see reference to function template instantiation 'void std::sort<std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>,topCompetitors::<lambda_885448e6a7e9e4442c4f55247636e75d>>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Ty=std::pair<int,std::string>,
1> _RanIt=std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<int,std::string>>>>>,
1> _Pr=topCompetitors::<lambda_885448e6a7e9e4442c4f55247636e75d>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2672: '_Sort_unchecked': no matching overloaded function found
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2780: 'void std::_Sort_unchecked(_RanIt,_RanIt,_Diff,_Pr)': expects 4 arguments - 3 provided
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3173): note: see declaration of 'std::_Sort_unchecked'
Can you help me to understand what's wrong and how to fix it?
I want to sort by value subset from set
It means that I can sort only on random_iterator?
Or in that case are there any std methods to extract subset values to make vector out of them?
You don't sort std::set.
It "sorts" itself, by the values of its elements.
That is a fundamental property of std::set.
You can override the criteria by which it is sorted, by providing a replacement Compare (or specialising the default, std::less) (ref).
Or, you can use a different container.
Related
I'm trying to get my code that compiles on Linux to be compatible with Windows. Here is a simplification of my situation (I have a variant of more than one type in the real situation.):
#include "stdafx.h"
#include <variant>
#include <functional>
#include <string>
using namespace std;
auto searcher(const string& s) {
return boyer_moore_searcher(s.begin(), s.end());
}
int main()
{
string pattern = "ABC";
variant<decltype(searcher(""))> v{ searcher(pattern) };
}
When I compile with MSVC 2017, I get the following error:
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2603): error C2440: 'initializing': cannot convert from 'const std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>' to 'void *'
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2603): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2601): note: while compiling class template member function 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::boyer_moore_searcher(const std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>> &) noexcept'
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\variant(537): note: see reference to function template instantiation 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::boyer_moore_searcher(const std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>> &) noexcept' being compiled
1> with
1> [
1> _Ty=char
1> ]
1>d:\users\narut\source\repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp(12): note: see reference to class template instantiation 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>' being compiled
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2603): error C2439: 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::_Data': member could not be initialized
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2629): note: see declaration of 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::_Data'
1> with
1> [
1> _Ty=char
1> ]
The same code compiles with the libstdc++ implementation on gcc. I tried libc++ experimental with clang and it also compiles. Is this a MSVC implementation bug or is there no C++ standard that requires variant to work with this particular class?
The following code compiles just fine with VC 15.3.5:
#include <Windows.h>
#include <string>
#include <memory>
#include <optional>
#include <functional>
class TypeDictionary;
class IStructureField {};
using FieldFactory = std::function<std::unique_ptr<IStructureField>(TypeDictionary& dict, LPCSTR szName, std::optional<ULONG> ulOffset, std::optional<std::string> strFieldType, std::optional<ULONG> ulFieldSize)>;
int main()
{
FieldFactory ff;
ff = [](TypeDictionary& dict, LPCSTR szName, std::optional<ULONG> ulOffset, std::optional<std::string> szFieldType, std::optional<ULONG> ulFieldSize) -> std::unique_ptr<IStructureField> {
return std::unique_ptr<IStructureField>();
};
return 0;
}
It produces the following error message with 15.4.0:
1>f:\projects\fullpath\fullpath\fullpath.cpp(22): error C2679: binary '=': no operator found which takes a right-hand operand of type 'main::<lambda_36489dce1cc59e5a8de4329a29806b5a>' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\functional(656): note: could be 'std::function<std::unique_ptr<IStructureField,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)>::operator =(std::nullptr_t) noexcept'
1> with
1> [
1> _Ty=IStructureField
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\functional(629): note: or 'std::function<std::unique_ptr<IStructureField,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)>::operator =(std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &&)'
1> with
1> [
1> _Ty=IStructureField
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\functional(612): note: or 'std::function<std::unique_ptr<IStructureField,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)>::operator =(const std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &)'
1> with
1> [
1> _Ty=IStructureField
1> ]
1>f:\projects\fullpath\fullpath\fullpath.cpp(22): note: while trying to match the argument list '(FieldFactory, main::<lambda_36489dce1cc59e5a8de4329a29806b5a>)'
1>Done building project "FullPath.vcxproj" -- FAILED.
As I saw on cppcon (Stephan's talk), a lot of work has been done on <functional>... Could this be related? Any mistake I made?
Workarounds would be fine too.
Edit1: Added the two include files to help understand the issue is not there: Same error...
Edit2: After I repaired my VS installation, the code just compiled. However, the original code did not. I had bumped in https://developercommunity.visualstudio.com/content/problem/74313/compiler-error-in-xsmf-control-with-stdoptional-pa.html before and attempted the workaround (which exposed the issue I faced). Without the workaround, the code compiles (but hits the __this issue again...).
I'm trying to pinpoint where the location of this error is coming from. C2664 is a parameter conversion problem that isn't showing up as an error in my coding, but rather something that is written in xmemory0 (or at least, that's how i'm interpreting it). I understand that part of what it's complaining about is that it is being asked to convert from an allocator to a string. I'm not using an allocator in this file.
The error reads:
C2664 'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty>
&&)': cannot convert argument 1 from
'std::vector<std::string,std::allocator<_Ty>>' to 'const std::string &'
Project1 c:\program files (x86)\microsoft visual studio
14.0\vc\include\xmemory0, Line 737
The code in question:
/*
Interpreter class implementation: utilizes the lex scanner class as
well as the expression evaluator to interpret simple statements like
read display and assignment statements
!!: indicates current problems or things that are going to be problems.
*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include "lexScanner.h"
#include "expEvaluator.h"
#include "interpreter.h"
using namespace std;
void Interpreter::executeProgram(vector<string>& sourceProgram)
{
// ****************************************************
// PARSING THE INFORMATION:
// Intialize all containers for storage of broken down string components
// Resize al appropriate vectors to the size of the source program
// ****************************************************
// !!: This probably wont work. Source program is going to be magnitudes
// smaller than the amount of category tokens
// but it worked in the last programming project soooo. .. .
vectOfTokenVects myVectOfTokenVects;
vectOfCategoryVects myVectOfCategoryVects;
perLineCategoryVector myPerLineCatVect;
// Send for parsing
LexicalScanner::getLexicalInfo(sourceProgram,myVectOfTokenVects,myVectOfCategoryVects);
//Display the lexical information for user's means
cout << "Here is a preview of the source code to be executed. . \n";
// Create a string-to-float map that serves as a symbol table to maintain
// the names of variables and their values during execution
// !!: figure out how to store items in the map
floatVarValueTable varTable;
// ****************************************************
// EXECUTING A READ STATEMENT:
// Obtain numerical input from the user and
// store it properly in the corresponding variable in the map as a symbol
// table.
// ****************************************************
// Cycle through the tokens, searching for the three categories
// If the catToken is keyword AND IS read, go ahead and execute a cin
// If the catToken is keyword AND IS display, go ahead and execute a cout
// If the catToken is an assignment statement, figure out how far the
// math expression goes. Try to compute the line. Search comma to comma?
// ****************************************************
for (int i = 0; i < myVectOfCategoryVects.size(); i++)
{
// Start out with the largest net: search for keywords
for (int j = 0; j < myVectOfCategoryVects[i].size(); j++)
{
if (myVectOfCategoryVects[i][j] == KEYWORD)
{
// SCENARIO 1: READ STATEMENT
// Have the user enter the value for the variable to be added to vartable.
if ((myVectOfTokenVects[i][j] == "Read") || (myVectOfTokenVects[i][j] == "READ") || (myVectOfTokenVects[i][j] == "read"))
{
float reportedValue;
cout << "Please enter a value for: " << myVectOfTokenVects[i][j + 1] << endl;
cin >> reportedValue;
string dumpedString = myVectOfTokenVects[i][j + 1];
varTable.emplace(dumpedString, reportedValue);
}
// SCENARIO 2: DISPLAY STATEMENT
// Search the vector (token vector) for things to display.
if (myVectOfTokenVects[i][j] == "Display" || myVectOfTokenVects[i][j] == "display" || myVectOfTokenVects[i][j] == "DISPLAY")
{
// Search the vector continiously until you reach a comma or semicolon. Both comma and semicolon indicate the end of the display statement.
int currentPosition = j+1;
for (int j = currentPosition; j > myVectOfTokenVects[i].size(); i++)
{
if (myVectOfCategoryVects[i][j] != COMMA)
{
cout << myVectOfTokenVects[i][j];
}
// We've got something after the comma, prob an expression. Send it to expression evaluator
if (myVectOfCategoryVects[i][j] == COMMA)
{
expVector infixExpression;
float expValue;
// Start filling up the expression vector
for (int k = j; k > myVectOfTokenVects[i][j].size(); k++)
{
infixExpression.push_back(myVectOfTokenVects[i][k]);
}
// Take the newly formed expression vector and send it to the evaluator. It does pass by reference, so the values in the vartable and infixexpression are going to be updated
ExpressionEvaluator::infixEvaluator(infixExpression, varTable, expValue);
cout << expValue;
}
}
}
}
// ****************************************************
// SEARCHING FOR VARIABLES
// If the token in question is an assignment operator, go ahead
// and insert the item before and after it.
// Note: This leaves a hole stating that if there is a floating
// equals sign somewhere with nothing before or after it, it
// could cause a out of bounds error.
// Note: We should also check for the presence of an equation.
// Evaluate the RHS of the assignment op
if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
{
// SCENARIO 1: SIMPLE EVALUATION
// If it's a simple 3 part assignment statement: ie:
// name (=) value, go ahead and store it
if (myVectOfCategoryVects[i][j + 2])
{
// Make sure the assignment op has a var name
if (myVectOfCategoryVects[i][j - 1])
{
// Store both the name and value
varTable.emplace(myVectOfTokenVects[j - 1], myVectOfTokenVects[j + 1]);
}
}
}
// SCENARIO 2: COMPLEX EVALUATION
// More complex evaluation: sending the RHS to the evaluator
// NOTE: postfix evaluator needs to be fixed. .
if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
{
// Semicolon out of normal place, assume incoming expression
if (myVectOfCategoryVects[i][j + 2] != SEMICOLON)
{
// NOTE: The placement of this is intentional. . .it guarentees that they get cleared on OOS
expVector infixExpression;
float expValue;
// Start with whatever is before the assignment op, start adding items to the infixExpression vector. Lookout for semicolon
for (int j = 0; myVectOfCategoryVects[i][j] != SEMICOLON; i++)
{
infixExpression.push_back(myVectOfTokenVects[i][j]);
}
// Take the newly formed expression vector and send it to the evaluator. It does pass by reference, so the values in the vartable and infixexpression are going to be updated
ExpressionEvaluator::infixEvaluator(infixExpression, varTable, expValue);
}
}
}
}
}
Relative definitions:
//****************************************************************************
// define a mnemonic name for the type to store tokens of one line of statement
//****************************************************************************
typedef vector<string> perLineTokenVector;
//****************************************************************************
// define a mnemonic name for the type to store tokens of lines of statement
//****************************************************************************
typedef vector<perLineTokenVector> vectOfTokenVects;
//****************************************************************************
// define a mnemonic name for the type to store categories of tokens
// of one line of statement
//****************************************************************************
typedef vector<tokenCategory> perLineCategoryVector;
//****************************************************************************
// define a mnemonic name for the type to store categories of tokens
// of one line of statement
//****************************************************************************
typedef vector<perLineCategoryVector> vectOfCategoryVects;
edit: The error seems to reside in this area
if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
{
// SCENARIO 1: SIMPLE EVALUATION
// If it's a simple 3 part assignment statement: ie:
// name (=) value, go ahead and store it
if (myVectOfCategoryVects[i][j + 2]=SEMICOLON)
{
// Make sure the assignment op has a var name
if (myVectOfCategoryVects[i][j - 1] = STRING_LITERAL)
{
string stringDump = myVectOfTokenVects[i][j - 1];
// Store both the name and value
varTable.emplace(stringDump, myVectOfTokenVects[i][j + 1]);
}
}
}
Build output
1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1> interpreter.cpp
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(67): warning C4018: '<': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(70): warning C4018: '<': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(94): warning C4018: '>': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(106): warning C4018: '>': signed/unsigned mismatch
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): error C2664: 'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty> &&)': cannot convert argument 2 from 'std::string' to 'const float &'
1> with
1> [
1> _Kty=std::string,
1> _Ty=float
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: Reason: cannot convert from 'std::string' to 'const float'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,std::string&,std::string&>(_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,std::string&,std::string&>(_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(996): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::string&,std::string&>(std::allocator<_Other> &,_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Tree_node<std::pair<const std::string,float>,void *>>,
1> _Ty=std::pair<const std::string,float>,
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(995): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::string&,std::string&>(std::allocator<_Other> &,_Objty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Tree_node<std::pair<const std::string,float>,void *>>,
1> _Ty=std::pair<const std::string,float>,
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Objty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(889): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,std::string&,std::string&>(_Ty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Ty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(887): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,std::string&,std::string&>(_Ty *,std::string &,std::string &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1> _Ty=std::pair<const std::string,float>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(1076): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::string&,std::string&>(std::string &,std::string &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Traits=std::_Tmap_traits<std::string,float,std::less<std::string>,std::allocator<std::pair<const std::string,float>>,false>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(1076): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::string&,std::string&>(std::string &,std::string &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Traits=std::_Tmap_traits<std::string,float,std::less<std::string>,std::allocator<std::pair<const std::string,float>>,false>
1> ]
1> c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(145): note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::string&,std::basic_string<char,std::char_traits<char>,std::allocator<char>>&>(std::string &,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std::pair<const std::string,float>>
1> ]
1> c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(145): note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::string&,std::basic_string<char,std::char_traits<char>,std::allocator<char>>&>(std::string &,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=float,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std::pair<const std::string,float>>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
edit 3
It appears that the problem was with what I was passing to the emplacement function for the map. myVectOfTokenVects is a fancy string vector of string vectors. The second argument to the map.emplacement() requires a float value, of which myVectOfTokenVects cannot supply. The problem has been found.
Follow-up:
How would I go about parsing the string value to cast it to a float for map.emplacement()?
I am using VS2012 and I have problem with following example:
#include <chrono>
#include <thread>
int main()
{
// doesn't compile and I don't understand why:
std::this_thread::sleep_for(std::chrono::duration<double>(0.1));
// I can use this but still I would like to know the reason:
std::this_thread::sleep_for(std::chrono::duration<long long, std::milli>(100));
return 0;
}
Both duration should be valid. And it is possible to use them in different context.
Compile error:
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(749): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'const std::chrono::duration<_Rep>' (or there is no acceptable conversion)
1> with
1> [
1> _Rep=double
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(166): could be 'std::chrono::duration<_Rep,_Period> &std::chrono::duration<_Rep,_Period>::operator +=(const std::chrono::duration<_Rep,_Period> &)'
1> with
1> [
1> _Rep=__int64,
1> _Period=std::nano
1> ]
1> while trying to match the argument list '(std::chrono::nanoseconds, const std::chrono::duration<_Rep>)'
1> with
1> [
1> _Rep=double
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(164) : see reference to function template instantiation 'xtime std::_To_xtime<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled
1> with
1> [
1> _Nx=0x01,
1> _Rep=double
1> ]
1> i:\prog\.c++\test2\test2\source.cpp(13) : see reference to function template instantiation 'void std::this_thread::sleep_for<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled
1> with
1> [
1> _Nx=0x01,
1> _Rep=double
1> ]
1>
1>Build FAILED.
Any help appreciated.
It is VS2012 compiler issue. Not 100% sure if it is this one (thx Praetorian). But it is possible to compile without problem with gcc 4.9.2. I should've think about trying it before I ask.
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.