2 Replies - 638 Views - Last Post: 03 January 2013 - 07:57 AM Rate Topic: -----

#1 squarepenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-May 12

cast error when accessing map<pair<int,int>,bool[4]>

Posted 03 January 2013 - 07:43 AM

I'm trying to set up a data-structure that can access and add arrays of four booleans when given a two-dimensional coordinate in the form of pair<int,int>.

code:
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;

class Data{
private:
	map<pair<int,int>,bool[4]> datamap;
public:
	bool* get(pair<int,int> in){
		return datamap.at(in);
	}
	void add(pair<int,int> in, bool b[4]){
		copy(b,b+4,datamap[in]);
	}
};

int main(){
	Data d;
	bool b[] = {true,false,true,false};
	d.add(make_pair(0,0),B)/>/>/>/>;
	cout<<d.get(make_pair(0,0))<<endl;
	cin.get();
}


But when I try to compile it, I get an error that makes no sence.

error:
error C2440: '<function-style-cast>' : cannot convert from 'int' to 'bool [4]'	c:\program files (x86)\microsoft visual studio 11.0\vc\include\map	198	1	robotTwo



Which points me to the 9th line in this snippet from include\map:
mapped_type& operator[](const key_type& _Keyval)
	{	// find element matching _Keyval or insert with default mapped
	iterator _Where = this->lower_bound(_Keyval);
	if (_Where == this->end()
		|| this->_Getcomp()(_Keyval, this->_Key(_Where._Mynode())))
		_Where = this->insert(_Where,
			pair<key_type, mapped_type>(
				_Keyval,
				mapped_type()));
	return (_Where->second);
	}


which is gibberish to me.

Can someone tell me what is going on?

entire output:
1>------ Build started: Project: robotTwo, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(198): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'bool [4]'
1>          There are no conversions to array types, although there are conversions to references or pointers to arrays
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(191) : while compiling class template member function 'bool (&std::map<_Kty,_Ty>::operator [](const std::pair<_Ty1,_Ty2> &))[4]'
1>          with
1>          [
1>              _Kty=std::pair<int,int>,
1>              _Ty=bool [4],
1>              _Ty1=int,
1>              _Ty2=int
1>          ]
1>          f:\school\taalderwiskunde\robottwo\robottwo\main.cpp(14) : see reference to function template instantiation 'bool (&std::map<_Kty,_Ty>::operator [](const std::pair<_Ty1,_Ty2> &))[4]' being compiled
1>          with
1>          [
1>              _Kty=std::pair<int,int>,
1>              _Ty=bool [4],
1>              _Ty1=int,
1>              _Ty2=int
1>          ]
1>          f:\school\taalderwiskunde\robottwo\robottwo\main.cpp(8) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=std::pair<int,int>,
1>              _Ty=bool [4]
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Is This A Good Question/Topic? 0
  • +

Replies To: cast error when accessing map<pair<int,int>,bool[4]>

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2127
  • View blog
  • Posts: 3,260
  • Joined: 21-June 11

Re: cast error when accessing map<pair<int,int>,bool[4]>

Posted 03 January 2013 - 07:53 AM

You can't use C arrays as values in a map because map values need to be default constructible and assignable, which arrays are not. You can use std::vectors or, if you're using C++11, std::arrays instead.
Was This Post Helpful? 1
  • +
  • -

#3 squarepenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-May 12

Re: cast error when accessing map<pair<int,int>,bool[4]>

Posted 03 January 2013 - 07:57 AM

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1