1

Line 29 is choking in compile:

void
CI_Metadata::get_record_metadata(const char* block, RecordInfoVector* record_info_vector) {
    *record_info_vector = this->records.at(block); // <== LINE 29
    return;
}

The error is:

CI_Metadata.cpp: In member function ‘void CI_Metadata::get_record_metadata(const char*, RecordInfoVector*)’:
CI_Metadata.cpp:29: error: invalid conversion from ‘const char*’ to ‘char*’
CI_Metadata.cpp:29: error:   initializing argument 1 of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::at(const _Key&) [with _Key = char*, _Tp = std::vector<std::pair<char*, bool>, std::allocator<std::pair<char*, bool> > >, _Compare = std::less<char*>, _Alloc = std::allocator<std::pair<char* const, std::vector<std::pair<char*, bool>, std::allocator<std::pair<char*, bool> > > > >]’

I assume this means that at() won't take a const char *. How, then, do I access the vector stored at this->records.at(block) where block is a const char*?

Chap
  • 3,649
  • 2
  • 46
  • 84

3 Answers3

2

I think the easiest way to fix this and a host of potential other issues is by turing std::map<char*, ...> into std::map<std::string, ...>.

Alternatively, define a suitable comparator: Using char* as a key in std::map

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

If you can change the at() function, read NPE's answer.

Assuming you don't have control over the at() function but do have control over your get_record_metadata prototype you can just change your parameter to no longer be declared as a const.

void
CI_Metadata::get_record_metadata(char* block, RecordInfoVector* record_info_vector) {

If you must pass a const char* into get_record_metadata your only alternative is to copy data into another char* that isn't a const. http://en.cppreference.com/w/cpp/string/byte/strcpy

seanmk
  • 1,934
  • 15
  • 28
  • 2
    Unfortunately, I *must* pass const char \*. The author of get_record_metadata() can't or won't think beyond char*. His code isn't ready yet so I'm writing a rough approximation, which obviously must conform to the API. (However, this will serve as another argument for him to start using std::string - I had no idea that char\* would be such a second-class citizen in the modern std:: world.) – Chap Jul 20 '13 at 07:10
0

Because you can imlicitly assign a variable of less qualifier to more qualifier But you cant implicitly assign a variable of MORE qualifier to less qualifier

for example

foo(char * p) fooc(const char * p)

int main(int argc, char agrv[]) {
 const char* cp = "hello";
 char* p = new char[10];
 foo(cp); // ==> compilation error
 strcpy(p,  cp);
 fooc(p) // No probs assigning to a more qualified var 
 }

In your case either convert const char* block to char* block


or


cast away the const like this

char* newblock = const_cast<char*>(block)//cast away const's ness 
*record_info_vector = this->records.at(newblock);
Anand Rathi
  • 790
  • 4
  • 11