distance calculation error in c++ [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int square(int a){
return a*a;
}
struct Point{
int x,y;
};
int distance (const Point& a,const Point& b){
int k=(int) sqrt((float)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
return k;
}
int main(){
vector<Point>a(10);
for (int i=0;i<10;i++){
cin>>a[i].x>>a[i].y;
}
int s=0;
int s1;
int k=0;
for (int i=1;i<10;i++){
s+=square(distance(a[0],a[i]));
}
for (int i=1;i<10;i++){
s1=0;
for (int j=0;j<10;j++){
s1+=square(distance(a[i],a[j]));
if (s1<s) { s=s1; k=i;}
}
}
cout<<k<<"Points are:";
cout<<a[k].x;
cout<<a[k].y;
return 0;
}
i have following code but here is list of errors
1>------ Build started: Project: distance, Configuration: Debug Win32 ------
1> distance.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C2039: 'iterator_category' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(30) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(373) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C2039: 'value_type' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C2146: syntax error : missing ';' before identifier 'value_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(374) : see declaration of 'std::iterator_traits<_Iter>::value_type'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C2868: 'std::iterator_traits<_Iter>::value_type' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C2039: 'difference_type' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C2146: syntax error : missing ';' before identifier 'difference_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C2602: 'std::iterator_traits<_Iter>::difference_type' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(375) : see declaration of 'std::iterator_traits<_Iter>::difference_type'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C2868: 'std::iterator_traits<_Iter>::difference_type' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C2039: 'pointer' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C2146: syntax error : missing ';' before identifier 'pointer'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C2602: 'std::iterator_traits<_Iter>::pointer' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(377) : see declaration of 'std::iterator_traits<_Iter>::pointer'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C2868: 'std::iterator_traits<_Iter>::pointer' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C2039: 'reference' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C2146: syntax error : missing ';' before identifier 'reference'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C2602: 'std::iterator_traits<_Iter>::reference' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(378) : see declaration of 'std::iterator_traits<_Iter>::reference'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C2868: 'std::iterator_traits<_Iter>::reference' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This is caused in part by your use of using namespace std;.
There is a function in the standard library, std::distance, that is used to compute the distance between two iterators into a container. That function is being selected during overload resolution instead of your distance function.
If you don't use using namespace std;, you won't have this problem. Your use of using namespace std; brings all of the names from the std namespace into the global namespace. This is bad because there are a lot of common names in namespace std and it can cause convoluted issues during name lookup, as you've discovered here.
In general, prefer not to use using namespace, especially not at file scope. It's far easier and clearer in the long run to qualify each of the names that you want to use.

#James is right in that getting rid of using namespace std will make this problem go away. But that's not exactly what the actual problem is.
The problem comes from the way names are resolved in C++. C++ uses a very aggressive approach to resolving name lookups when resolving the names of free functions if no suitable class member was found. This is called Argument-dependent name lookup (or ADL), sometimes referred to as Koenig lookup. And at the same time, it is one of the things makes C++ so powerful, but also so deadly and confounding.
Here's the basic problem, simplified.
You call vector::operator[] which returns a vector::reference or a vector::const_reference. You take this return value and call distance() unqualified.
Because distance wasn't found in and applicable class, ADL kicks in, which goes out and gets too many potential matching names.
ADL then goes through the list of candidates and picks "the best match". In this case, the reference that operator[] returns is a closer match to the type expected by std::distance than by your distance, although both are compatible, so std::distance is selected.
Several ways to solve this problem:
Don't using namespace std. Instead, using just the peices you want.
Quantify explicitly which distance you refer to using the scope resolution operator: ::distance()
Change distance to take pointers instead of const &. This effectively turns off ADL.:
Example:
int distance(const Point* a,const Point* b)
s+=square(distance(&a[0],&a[1]));
Additional Resources:
"A Modest Proposal: Fixing ADL (revision 2)" by Herb Sutter

Related

Having trouble making a thread for a member function with parameters | C++ | WxWidgets

My thread gives me no errors before compilation, but upon compilation, I get this error...
Error code:
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(55,14): error C2672: 'invoke': no matching overloaded function found
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(62): message : see reference to function template instantiation 'unsigned int std::thread::_Invoke<_Tuple,0,1,2,3,4>(void *) noexcept' being compiled
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(66): message : see reference to function template instantiation 'unsigned int (__cdecl *std::thread::_Get_invoke<_Tuple,0,1,2,3,4>(std::integer_sequence<size_t,0,1,2,3,4>) noexcept)(void *) noexcept' being compiled
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(89): message : see reference to function template instantiation 'void std::thread::_Start<_Ty,std::vector<std::string,std::allocator<std::string>>&,std::vector<std::string,std::allocator<std::string>>&,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>&,std::vector<std::string,std::allocator<std::string>>&>(_Fn &&,std::vector<std::string,std::allocator<std::string>> &,std::vector<std::string,std::allocator<std::string>> &,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>> &,std::vector<std::string,std::allocator<std::string>> &)' being compiled
1> with
1> [
1> _Ty=void (__cdecl MyFrame::* )(std::vector<std::string,std::allocator<std::string>>,std::vector<std::string,std::allocator<std::string>>,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>,std::vector<std::string,std::allocator<std::string>>),
1> _Fn=void (__cdecl MyFrame::* )(std::vector<std::string,std::allocator<std::string>>,std::vector<std::string,std::allocator<std::string>>,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>,std::vector<std::string,std::allocator<std::string>>)
1> ]
1>C:\Users\Me\C++\Data-Mange-Bot-C++ver\DataManageApp\main.cpp(2578): message : see reference to function template instantiation 'std::thread::thread<void(__cdecl MyFrame::* )(std::vector<std::string,std::allocator<std::string>>,std::vector<std::string,std::allocator<std::string>>,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>,std::vector<std::string,std::allocator<std::string>>),std::vector<std::string,std::allocator<std::string>>&,std::vector<std::string,std::allocator<std::string>>&,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>&,std::vector<std::string,std::allocator<std::string>>&,0>(_Fn &&,std::vector<std::string,std::allocator<std::string>> &,std::vector<std::string,std::allocator<std::string>> &,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>> &,std::vector<std::string,std::allocator<std::string>> &)' being compiled
1> with
1> [
1> _Fn=void (__cdecl MyFrame::* )(std::vector<std::string,std::allocator<std::string>>,std::vector<std::string,std::allocator<std::string>>,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>,std::vector<std::string,std::allocator<std::string>>)
1> ]
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(51,5): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Ty1 &&,_Types2 &&...) noexcept(<expr>)'
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\type_traits(1482): message : see declaration of 'std::invoke'
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(51,5): message : With the following template arguments:
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(51,5): message : '_Callable=void (__cdecl MyFrame::* )(std::vector<std::string,std::allocator<std::string>>,std::vector<std::string,std::allocator<std::string>>,std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>,std::vector<std::string,std::allocator<std::string>>)'
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(51,5): message : '_Ty1=std::vector<std::string,std::allocator<std::string>>'
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(51,5): message : '_Types2={std::vector<std::string,std::allocator<std::string>>, std::vector<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>,std::allocator<std::variant<std::string,std::vector<std::string,std::allocator<std::string>>>>>, std::vector<std::string,std::allocator<std::string>>}'
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\thread(51,5): error C2780: 'unknown-type std::invoke(_Callable &&) noexcept(<expr>)': expects 1 arguments - 5 provided
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\type_traits(1476): message : see declaration of 'std::invoke'
This is despite following the solution provided in this question. Although the difference here is that I have multiple parameters and I'm starting the thread inside another member function.
I've looked at online resources and haven't turned up anything, if anyone can help that would be great.
Code:
class MyFrame : public wxFrame{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void RightSearchBarTextChange(wxCommandEvent& event);
void RightSearchResultsCreate(std::vector<std::string> CategoryFilterMap, std::vector<std::string> SubCategoryFilterMap, std::vector<std::variant<std::string, std::vector<std::string>>> DateFilterMap, std::vector<std::string> TagFilterMap);
}
void MyFrame::RightSearchBarTextChange(wxCommandEvent& event) {
std::thread thread_object(&MyFrame::RightSearchResultsCreate, RightSearchCategoryFilterSelectedVector, RightSearchSubCategoryFilterSelectedVector, RightSearchDateFilterSelectedVector, RightSearchTagFilterSelectedVector);
thread_object.join();
}
void MyFrame::RightSearchResultsCreate(std::vector<std::string> CategoryFilterMap, std::vector<std::string> SubCategoryFilterMap, std::vector<std::variant<std::string, std::vector<std::string>>> DateFilterMap, std::vector<std::string> TagFilterMap) {
// do stuff here
}

Unexpected template instantiation leading to a compile error

I just wonder why my code cannot be compiled. Is the below okay? I'm trying to declare a simple class with Category1 and Category2 typedef-s.
Category1 typedef compiles fine but Category2 one does not.
It seems that Category2 typedef cannot be compiled because class iterator_traits<> is instantiated despite the surrounding class X is not instantiated... Seems very confusing for me.
#include <iterator>
template <class GetterFunType>
struct X {
GetterFunType containerGetterFun;
// works
typedef typename std::iterator_traits<typename GetterFunType::iterator>::iterator_category Category1;
// compile error - std::iterator_traits<> is instantiated with type 'unknown'
typedef typename std::iterator_traits<
decltype(containerGetterFun().begin())>::iterator_category Category2;
X(GetterFunType _containerGetterFun) : containerGetterFun(_containerGetterFun) { }
};
Note that I do not need to instantiate class X to get the following errors (the above is the complete compilation unit).
In Visual Studio 2012 I'm getting this:
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1> c:\data\fsl\apif_src_review\apif_src\systemns.cpp(11) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=unknown
1> ]
1> c:\data\fsl\apif_src_review\apif_src\systemns.cpp(14) : see reference to class template instantiation 'X<GetterFunType>' being compiled
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C3254: 'std::iterator_traits<_Iter>' : class contains explicit override 'iterator_category' but does not derive from an interface that contains the function declaration
1> with
1> [
1> _Iter=unknown
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2838: 'iterator_category' : illegal qualified name in member declaration
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=unknown
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1> with
1> [
1> _Iter=unknown
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=unknown
1> ]
In xutility(364) there is:
template<class _Iter>
struct iterator_traits
{ // get traits from iterator _Iter
typedef typename _Iter::iterator_category iterator_category;
typedef typename _Iter::value_type value_type;
typedef typename _Iter::difference_type difference_type;
typedef difference_type distance_type; // retained
typedef typename _Iter::pointer pointer;
typedef typename _Iter::reference reference;
};
My case is that I want to declare a class that gets a lambda in constructor. The lambda is expected to return a reference to a container. And I need to determine wheter the returned container has a random-access iterator. But I got stuck with this compilation error. Thank you for explanation!
I was able to compile the same code without any errors using gcc 5.3.1, with -std=c++11
Your compiler is a relatively old compiler that does not support the current C++1x standard. Switching to another compiler is the only option I can see here, if you need to use modern C++ features.
I see that the code is okay but there are some Visual Studio 2012 limitations not allowing to compile. At least in MSVS 2015 it works, same as for gcc 5.3.1. Thanks, friends!

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.

Boost compiler error?

I am trying to build this program:
#include "stdafx.h"
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
using namespace std;
int main()
{
using boost::interprocess;
// Create the file mapping
file_mapping fm("input.dat", read_only);
// Map the file in memory
mapped_region region(fm, read_only);
// Get the address where the file has been mapped
float * addr = (float *)region.get_address();
std::size_t elements = region.get_size() / sizeof(float);
}
But I am having two problems, for the main I am getting:
1>tasker.cpp(98): error C2873: 'boost::interprocess' : symbol cannot be used in a using-declaration
1>tasker.cpp(101): error C2065: 'file_mapping' : undeclared identifier
1>tasker.cpp(101): error C2146: syntax error : missing ';' before identifier 'fm'
1>tasker.cpp(101): error C2065: 'read_only' : undeclared identifier
1>tasker.cpp(101): error C3861: 'fm': identifier not found
1>tasker.cpp(103): error C2065: 'mapped_region' : undeclared identifier
1>tasker.cpp(103): error C2146: syntax error : missing ';' before identifier 'region'
1>tasker.cpp(103): error C2065: 'fm' : undeclared identifier
1>tasker.cpp(103): error C2065: 'read_only' : undeclared identifier
1>tasker.cpp(103): error C3861: 'region': identifier not found
1>tasker.cpp(105): error C2065: 'region' : undeclared identifier
1>tasker.cpp(105): error C2228: left of '.get_address' must have class/struct/union
1> type is 'unknown-type'
1>tasker.cpp(106): error C2065: 'region' : undeclared identifier
1>tasker.cpp(106): error C2228: left of '.get_size' must have class/struct/union
1> type is 'unknown-type'
and for the #include <boost/interprocess/mapped_region.hpp> I am getting
1>C:\Users\Mike\Documents\boost_1_55_0\boost/intrusive/detail/has_member_function_callable_with.hpp(200): error C2228: left of '.select_on_container_copy_construction' must have class/struct/union
1> type is 'boost::move_detail::add_rvalue_reference<U>::type'
1> C:\Users\Mike\Documents\boost_1_55_0\boost/intrusive/detail/has_member_function_callable_with.hpp(276) : see reference to class template instantiation 'boost::container::container_detail::has_member_function_callable_with_select_on_container_copy_construction_impl<Fun,true,>' being compiled
1> with
1> [
1> Fun=std::allocator<std::pair<const boost::interprocess::ipcdetail::sync_id *const ,boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const boost::interprocess::ipcdetail::sync_id,void *>>>>>
1> ]
1> C:\Users\Mike\Documents\boost_1_55_0\boost/container/allocator_traits.hpp(262) : see reference to class template instantiation 'boost::container::container_detail::has_member_function_callable_with_select_on_container_copy_construction<const Alloc,>' being compiled
1> with
1> [
1> Alloc=std::allocator<std::pair<const boost::interprocess::ipcdetail::sync_id *const ,boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const boost::interprocess::ipcdetail::sync_id,void *>>>>>
1> ]
1> C:\Users\Mike\Documents\boost_1_55_0\boost/container/detail/tree.hpp(217) : see reference to class template instantiation 'boost::container::allocator_traits<A>' being compiled
1> with
1> [
1> A=std::allocator<std::pair<const boost::interprocess::ipcdetail::sync_id *const ,boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const boost::interprocess::ipcdetail::sync_id,void *>>>>>
1> ]
1> C:\Users\Mike\Documents\boost_1_55_0\boost/container/detail/tree.hpp(246) : see reference to class template instantiation 'boost::container::container_detail::intrusive_rbtree_type<A,boost::container::container_detail::tree_value_compare<Key,std::pair<const Key,T>,Compare,boost::container::container_detail::select1st<std::pair<const Key,T>>>>' being compiled
1> with
1> [
1> A=std::allocator<std::pair<const boost::interprocess::ipcdetail::sync_id *const ,boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const boost::interprocess::ipcdetail::sync_id,void *>>>>>
1> , Key=const boost::interprocess::ipcdetail::sync_id *
1> , T=boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const boost::interprocess::ipcdetail::sync_id,void *>>>
1> , Compare=boost::interprocess::ipcdetail::sync_handles::address_less
1> ]
1> C:\Users\Mike\Documents\boost_1_55_0\boost/container/map.hpp(83) : see reference to class template instantiation 'boost::container::container_detail::rbtree<Key,std::pair<const Key,T>,boost::container::container_detail::select1st<std::pair<const Key,T>>,Compare,Allocator>' being compiled
1> with
1> [
1> Key=const boost::interprocess::ipcdetail::sync_id *
1> , T=boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const boost::interprocess::ipcdetail::sync_id,void *>>>
1> , Compare=boost::interprocess::ipcdetail::sync_handles::address_less
1> , Allocator=std::allocator<std::pair<const boost::interprocess::ipcdetail::sync_id *const ,boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const boost::interprocess::ipcdetail::sync_id,void *>>>>>
1> ]
1> C:\Users\Mike\Documents\boost_1_55_0\boost/interprocess/sync/windows/sync_utils.hpp(226) : see reference to class template instantiation 'boost::container::map<const boost::interprocess::ipcdetail::sync_id *,boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<T>>,boost::interprocess::ipcdetail::sync_handles::address_less,std::allocator<std::pair<const Key,boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<T>>>>>' being compiled
1> with
1> [
1> T=std::pair<const boost::interprocess::ipcdetail::sync_id,void *>
1> , Key=const boost::interprocess::ipcdetail::sync_id *
1> ]
I am using Visual Studio Express 2013 for Windows Desktop, and boost 1.55.0.
I tried other boost libraries and work without any issue.... Also, I am using the x64 release build.
Boost 1.55 does not fully supports Visual Studio 2013 compiler yet.
Either:
use Visual Studio 2012 toolchain
or try to get latest source from svn co http://svn.boost.org/svn/boost/trunk boost-trunk
or patch boost yourself (don't forget to upload your patch)
or wait until someone will patch it. You can speed it up by submitting bug report
Edit
Just checked boost-trunk, with boost/intrusive/detail/has_member_function_callable_with.hpp already patched and it compiles fine with both vs2013 and vs2013-nov-2013-ctp toolchains. So, try it. And, if you have multiple boost versions, don't forget to change include paths (in Makefile or VC project properties) as I did. ;)
Note, that, obviously, latest development code can be unstable. Do not use it for end-user production.
Try changing this:
using boost::interprocess;
to this:
using namespace boost::interprocess;
(Ref: Example in http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/quick_guide.html )
Yeah. you need to say "using namespace boost::interprocess;"
Fix that and you should be good.

implementation of hash table [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 12 years ago.
i have following code
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <hash_map>
#include <string>
#include <iterator>
#include <ostream>
using namespace std;
struct Equal
{
bool operator()(const char *s1,const char *s2)const
{
return std::strcmp(s1,s2)==0;
}
};
typedef std::hash_multimap<const char*,int,hash<const char*>,Equal>map_type;
void lookup(const map_type&Map,const char *str){
cout<<str<<":";
pair<map_type::const_iterator ,map_type::const_iterator>p=Map.equal_range(str);
for (map_type::const_iterator i=p.first;i!=p.second;++i)
cout << (*i).second<<" ";
}
int maain(){
map_type m;
m.insert(map_type::value_type("H", 1));
m.insert(map_type::value_type("H", 2));
m.insert(map_type::value_type("C", 12));
m.insert(map_type::value_type("C", 13));
m.insert(map_type::value_type("O", 16));
m.insert(map_type::value_type("O", 17));
m.insert(map_type::value_type("O", 18));
m.insert(map_type::value_type("I", 127));
lookup(M,"I");
lookup(M,"0");
lookup(M,"Rn");
return 0;
}
but here is errors
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template
1> c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal,
1> _Mfl=true
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\hash_map(273) : see reference to class template instantiation 'std::_Hash<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true>
1> ]
1> c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(22) : see reference to class template instantiation 'stdext::hash_multimap<_Kty,_Ty,_Tr,_Alloc>' being compiled
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'rebind' : is not a member of 'Equal'
1> c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(11) : see declaration of 'Equal'
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2143: syntax error : missing ';' before '<'
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'other' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(27): error C2238: unexpected token(s) preceding ';'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2039: 'bucket_size' : is not a member of 'std::hash<_Kty>'
1> with
1> [
1> _Kty=const char *
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2065: 'bucket_size' : undeclared identifier
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2039: 'allocator_type' : is not a member of 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>'
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal,
1> _Mfl=true
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2146: syntax error : missing ',' before identifier 'allocator_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2065: 'allocator_type' : undeclared identifier
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(186): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(187): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(188): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(189): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(190): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(191): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(192): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(193): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(197): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2146: syntax error : missing ';' before identifier 'iterator'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C3254: 'std::_Hash<_Traits>' : class contains explicit override 'type' but does not derive from an interface that contains the function declaration
1> with
1> [
1> _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true>
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2838: 'type' : illegal qualified name in member declaration
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2208: 'std::iterator' : no members defined using this type
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.96
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
please help
You have to look at each complier error and fix them one at a time. We can't take a file of code and fix each error. Learning to live and even on occasion love the abusive spouse that is your complier is part of the process.
All your errors seem related to the fact that you're not passing the right template arguments to hash_multimap. Take a look at hash_multimap's documentation. The last parameter, Ăˆqual` should be the allocator, not a function object, check out the 1st error:
c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template
c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
with
[
_Kty=const char *,
_Ty=int,
_Tr=std::hash,
_Alloc=Equal,
_Mfl=true
]
Edit:
If you've looked at SGI's hash_multimap documentation, you'll notice that the hash_map and hash_multimap classes are different between vendors because they are not standard. The next version of the standard comprises standard hashed containers. In the mean time, well, you'll have to write non portable code!