Usage problem on std::unordered_map
There is an error saying that unordered_map has no associated operator '->'
Any ideas why?
Thanks
Jack
Code:
#ifndef _OGREDETOURCROWDBASE_H_
#define _OGREDETOURCROWDBASE_H_
class OgreDetourCrowdBase {
public:
virtual Ogre::Real getAgentHeight(void) = 0;
virtual Ogre::Real getAgentRadius(void) = 0;
virtual int getNbAgents(void) { return 0; }
virtual int getMaxNbAgents(void) { return 0; }
};
#endif
std::unordered_map<std::string, OgreDetourCrowdBase*> mDetourCrowd;
OgreDetourCrowdBase *detourCrowd = new OgreDetourCrowd(mRecast);
OgreDetourCrowdBase *detourCrowdCB = new OgreDetourCrowdCB(mRecastCB);
OgreDetourCrowdBase *detourCrowdVNA = new OgreDetourCrowdVNA(mRecastVNA);
OgreDetourCrowdBase *detourCrowdLorry = new OgreDetourCrowdLorry(mRecastLorry);
mDetourCrowd["Worker"] = detourCrowd;
mDetourCrowd["CB"] = detourCrowdCB;
mDetourCrowd["VNA"] = detourCrowdVNA;
mDetourCrowd["Lorry"] = detourCrowdLorry;
ObjectBase *OgreRecastApplication::createOperators(Ogre::String name, Ogre::Vector3 position) {
if(mDetourCrowd["Worker"]->getNbAgents() >= mDetourCrowd["Worker"]->getMaxNbAgents()) {
Ogre::LogManager::getSingletonPtr()->logMessage("Error: Cannot create crowd agent for new Worker. Limit of "+Ogre::StringConverter::toString(mDetourCrowd->getMaxNbAgents())+" reached", Ogre::LML_CRITICAL);
throw new Ogre::Exception(1, "Cannot create crowd agent for new Worker. Limit of "+Ogre::StringConverter::toString(mDetourCrowd->getMaxNbAgents())+" reached", "OgreRecastApplication::getOrCreateCharacter("+name+")");
}
ObjectBase *worker = new Worker(name, mSceneMgr, mDetourCrowd, mDebugDraw, position);
mObjects.push_back(worker);
return worker;
}
Error 1 error C2819: type 'std::tr1::unordered_map<_Kty,_Ty>' does not have an overloaded member 'operator ->' e:\jacky\documents\visual studio 2010\projects\ogrecrowd\ogrecrowd\samples\src\ogrerecastapplication.cpp 379 1 OgreRecast
Error 2 error C2039: 'getMaxNbAgents' : is not a member of 'std::tr1::unordered_map<_Kty,_Ty>' e:\jacky\documents\visual studio 2010\projects\ogrecrowd\ogrecrowd\samples\src\ogrerecastapplication.cpp 379 1 OgreRecast
Error 3 error C2819: type 'std::tr1::unordered_map<_Kty,_Ty>' does not have an overloaded member 'operator ->' e:\jacky\documents\visual studio 2010\projects\ogrecrowd\ogrecrowd\samples\src\ogrerecastapplication.cpp 380 1 OgreRecast
Error 4 error C2039: 'getMaxNbAgents' : is not a member of 'std::tr1::unordered_map<_Kty,_Ty>' e:\jacky\documents\visual studio 2010\projects\ogrecrowd\ogrecrowd\samples\src\ogrerecastapplication.cpp 380 1 OgreRecast
Error 5 error C2664: 'Worker::Worker(Ogre::String,Ogre::SceneManager *,OgreDetourCrowd *,bool,Ogre::Vector3)' : cannot convert parameter 3 from 'std::tr1::unordered_map<_Kty,_Ty>' to 'OgreDetourCrowd *' e:\jacky\documents\visual studio 2010\projects\ogrecrowd\ogrecrowd\samples\src\ogrerecastapplication.cpp 384 1 OgreRecast
....
[/code]
Re: Usage problem on std::unordered_map
Reduce your program to the smallest and simplest program that you expect will compile but which demonstrates this error, then post the code.
EDIT:
Nevermind, I see it now: mDetourCrowd->getMaxNbAgents()
Basically, you have a typo. Note that it occurs twice.
Re: Usage problem on std::unordered_map
Quote:
Originally Posted by lucky6969b
There is an error saying that unordered_map has no associated operator '->'
How about MSDN? Or Google?
Compiler Error C2819
Re: Usage problem on std::unordered_map
main.cpp
Code:
#include "OgreDetourCrowdBase.h"
#include "OgreDetourCrowdCB.h"
#include <unordered_map>
#include <iostream>
using namespace std;
std::unordered_map<std::string, OgreDetourCrowdBase*> mDetourCrowd;
int main() {
OgreDetourCrowdBase *detourCB = new OgreDetourCrowdCB();
mDetourCrowd["CB"] = detourCB;
cout << mDetourCrowd["CB"]->getNbAgents() << endl;
}
OgreDetourCrowdBase.h
Code:
#ifndef _OGREDETOURCROWDBASE_H_
#define _OGREDETOURCROWDBASE_H_
class OgreDetourCrowdBase {
public:
virtual float getAgentHeight(void) = 0;
virtual float getAgentRadius(void) = 0;
virtual int getNbAgents(void) { return 0; }
virtual int getMaxNbAgents(void) { return 0; }
};
#endif
OgreDetourCrowdCB.h
Code:
#include "OgreDetourCrowdBase.h"
class OgreDetourCrowdCB : public OgreDetourCrowdBase {
float getAgentHeight(void) { return 5.0f; }
float getAgentRadius(void) { return 1.0f; }
int getNbAgents(void) { return 10; }
int getMaxNbAgents(void) { return 10; }
};
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 92 1 Test
Thank you laserlight
Jack
Re: Usage problem on std::unordered_map
Quote:
Originally Posted by
lucky6969b
...
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 92 1 Test
But is another error! Not a C2819 one!
https://www.google.com/search?source...a=1&gws_rd=ssl
http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
Re: Usage problem on std::unordered_map
Thanks Victor, The bug still exists. The test program runs fine, but the program I am working on does not.
I tried to overload the -> operator to no avail.
Jack
Re: Usage problem on std::unordered_map
Quote:
Originally Posted by
lucky6969b
I tried to overload the -> operator to no avail.
Jack
Define "no avail".
Re: Usage problem on std::unordered_map
Code:
std::unordered_map<std::string, OgreDetourCrowdBase*> mDetourCrowd;
Quote:
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 92 1 Test
Have you included <string> somewhere not shown in the code?
Re: Usage problem on std::unordered_map
Yes, all fixed, because of typos..... Thanks Victor and laserlight
Re: Usage problem on std::unordered_map
Quote:
Originally Posted by lucky6969b
I tried to overload the -> operator to no avail.
Why do you want to overload the -> operator? It seems to me that you just need to change mDetourCrowd->getMaxNbAgents() to mDetourCrowd["Worker"]->getMaxNbAgents().
Re: Usage problem on std::unordered_map
Yes, I spot that error too, it is all fine now.
Re: Usage problem on std::unordered_map
I didn't see that statement because it was too far down the right.
I got it now. It's typo as you said..
Thanks
Re: Usage problem on std::unordered_map
Quote:
Originally Posted by
lucky6969b
There is an error saying that unordered_map has no associated operator '->'
Any ideas why?
Get the book The C++ Standard Library, second edition, by Josuttis.
Then you can solve most C++ standard problems yourself like this one.