std::map error

This is a discussion on std::map error within the C++ Programming forums, part of the General Programming Boards category; I have a map that looks like this: Code: std::map<std::string, double> table; When I try this the compiler spews out ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    std::map error

    I have a map that looks like this:
    Code:
    std::map<std::string, double> table;
    When I try this the compiler spews out a bunch of error messages that I don't understand:

    Code:
    std::string name = "foo";
    std::map<std::string, double>::iterator it = table.find(name);
    if (it == table.end()) {std::cout << "Variable not found" << std::endl;}

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    What's the error messages?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    error: conversion from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> >’ to non-scalar type ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> >’ requested

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    From the error message, table is probably const is that context, so it should be:
    Code:
    std::map<std::string, double>::const_iterator it = table.find(name);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Yeah I figured it out. table is not declared const, but the member function this error occurred in was. I didn't think that would affect things like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21