Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the call to std::map::operator[] in this code?

I have the following typedef's in my code:

typedef unsigned long int ulint;
typedef std::map<ulint, particle> mapType;
typedef std::vector< std::vector<mapType> > mapGridType;

particle is a custom class with no default constructor.

VS2008 gives me an error in this code:

std::set<ulint> gridOM::ids(int filter)
{
    std::set<ulint> result;
    ulint curId;
    for ( int i = 0; i < _dimx; ++i ) {
        for ( int j = 0; j < _dimy; ++j ) {
            // next line is reported to be erroneous
            for ( mapType::iterator cur = objectMap[i][j].begin(); cur != objectMap[i][j].end(); ++cur )
            {
                curId = (*cur).first;
                if ( (isStatic(curId) && filter != 2) || (!isStatic(curId) && filter != 1) )
                {
                    result.insert(curId);
                }
            }
        }
    }
    return result;
}

objectMap is an object of mapGridType. The error reads:

error C2512: 'gridOM::particle::particle' : no appropriate default constructor available
while compiling class template member function 'gridOM::particle &std::map<_Kty,_Ty>::operator [](const unsigned long &)'  
        with  
        [  
            _Kty=ulint,  
            _Ty=gridOM::particle  
        ]  
        .\gridOM.cpp(114) : see reference to class template   instantiation 'std::map<_Kty,_Ty>'   being compiled  
        with  
        [  
            _Kty=ulint,  
            _Ty=gridOM::particle  
        ]  

Correct me if I'm wrong, but the above code should not be making calls to map::operator[] at all. The first operator[] call is made to vector< vector<mapType> > and returns a vector<mapType>, the second is made to vector<mapType> and returns a mapType aka a map<ulint, particle>, and I only call begin() and end on that map. So why do I get an error trying to compile the operator[] for map?

like image 466
suszterpatt Avatar asked Dec 21 '25 00:12

suszterpatt


2 Answers

I'm not sure of my answer. But when you instanciate a template, the compiler implements all the functions defined in the template (even if they aren't used). So you get the error, even if there is no use of the function.

Could you give the complete error message?

EDIT With the full error message, you get the problem! As you told, your object has no default constructor. However, in order to use a map, you need the default constructor (as it is required by the function operator[] in order to build an instance when using a new key), even if you don't use the function, it will be implemented by the compiler.

like image 177
Tristram Gräbener Avatar answered Dec 22 '25 16:12

Tristram Gräbener


The code you posted is perfectly fine and compiles without any errors at the for line in GCC and Comeau (Onlie) compilers. Right now I have no way to try it in VS2008 specifically, unfortunately.

If the error is indeed triggered by the for line, I'd suspect a problem in the compiler. There's a chance that the implementation of std::map<>::begin() or std::map<>::end() somehow depends on std::map<>::operator[]() in VS2008, but that would be rather strange.

like image 33
AnT Avatar answered Dec 22 '25 18:12

AnT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!