Being pretty green with C ++, I came across behavior that I donโt quite understand, and could not find an explanation even with an intensive search on Google, so I was hoping that someone could explain what was definitely wrong here.
#include <unordered_map>
typedef std::unordered_map<int, int> test_type;
class test
{
public:
static const test_type tmap;
};
#include "test.h"
const test_type test::tmap = {
{ 1, 1 }
};
#include "test.h"
int main()
{
std::cout << test::tmap[1];
std::cout << test::tmap.at(1);
return 0;
}
If I have code 1 in my code, the Visual Studio compiler insists that I use a binary operator [that has not been defined, but that makes no sense to me, since, as far as I know, it is not a binary operator [, (brackets always unary, right?).
So why does attempt 1 fail?
source
share