How to make default constructor appropriate for std::map?

I have a map of objects but I need to do something with the constructor apparently? No clue how this is done. Apparently I need to overload the < operator? I have to yet to need to overload an operator until now so I really don't know how to approach this. Any help would be greatly appreciated.
Normally, the option of overloading an operator implies that you have a custom data type. So for us to help very much, you must post the class and give more details in your question.

Aceix.
http://www.cplusplus.com/reference/map/map/

The third template parameter is a function used to compare the keys to sort the map. std::less returns arg1 < arg2, so your types must overload that operator.
You can:
- Overload the operator. It's not hard, just look for an example or post the code and we can help you.
- Use a different compare object.
- Use a different type for the key.
The objects being stored are from my class GTexture.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// GTexture.h
#pragma once

#include<SFML/Graphics.hpp>
#include<string>
// Encapsulate sf::Texture to have ID reference etc. This is basically purely for organizational purposes
class GTexture
{

public:
	sf::Texture texture; // The actual sf::Texture
	std::string nameRef; // I know I already have std::map for a key but this has other purposes so I still need it
	GTexture(const std::string file, const std::string name); // Automatically get the file texture is located in and game texture name
	~GTexture(void);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// GTexture.cpp
#include "GTexture.h"
#include<SFML/Graphics.hpp>
#include<iostream>
#include<string>

GTexture::GTexture(const std::string file, const std::string name) 
{
	GTexture::nameRef.assign(name);
	if(!GTexture::texture.loadFromFile(file.c_str())){
		std::cout << "Error: Could not " << file << " for GTexture " << name << " " << std::endl;
	}
}


GTexture::~GTexture(void)
{
	std::cout << "Mr. Destructor Here" << std::endl;
}
How do you want your overload to look like?

Aceix.
I don't entirely get what you mean by that question. I'm just trying to get this class compatible with std::map. Googling it I've seen people say that you need to overload the < operator but I don't how you overload it. I've seen operators overloaded before but I don't know what to do with < .
operator< is the less than operator, which some containers need in order to sort the contents (like keys in this case).

Since you are overloading a comparison operator, you'll need to return a bool:
 
bool operator<(const MyKey&, const MyKey&);


Note that I write MyKey. std::map sorts keys.
Topic archived. No new replies allowed.