Foo.h :
class Foo
{
public:
Foo(void);
~Foo(void);
void AddScreen(std::string name, ScreenBase &screenToAdd);
private:
std::map<std::string, ScreenBase> m_screens;
};
Foo.cpp :
void Foo::AddScreen(string name, ScreenBase &screenToAdd)
{
m_screens[name] = screenToAdd;
}
the last line creates a compile error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::
commenting out the last line and the compile succeeds.
I’m new to c++ (coming from a managed language) and don’t know why I can’t populate the map with this.
Any insight is appreciated. Thanks.
ScreenBase.h :
#pragma once
class ScreenBase
{
public:
ScreenBase();
~ScreenBase();
virtual void Update(float tt, float dt);
virtual void Render();
};
ScreenBase.cpp :
#include "pch.h"
#include "ScreenBase.h"
ScreenBase::ScreenBase(void)
{
}
ScreenBase::~ScreenBase(void)
{
}
void ScreenBase::Update(float tt, float dt)
{
}
void ScreenBase::Render()
{
}
You forgot to add the following line to Foo.cpp
#include <string>That should fix it.
The reason that fixes it is because the “<” operator between 2
std::stringobjects is defined there. Sincestd::mapis an associative array, it will sort the keys with either a specific sorting function you specify (as a third parameter for the template, e.g.std::map<int, MyObj, MyIntCompareFunctor>), or it will default to using < operator of the key type which in your case isstd::string.P.S. Also, pass strings by reference, not value:
e.g.
void foo(const std::string& bar){};