C++ class and method that search values in map

Manuel Egío

I have written a minimum class to better submit my problem.

I have three files:

1) test.hpp

#include <cstdlib>
#include <stdio.h>
#include <map>

class test {
  public:
    test () {}
    ~test () {}
    const char *getPin (const char *);
  private:
    static const std::map<const char *, const char *> pinIndex;
    static std::map<const char *, const char *> initializePins ();

};

2) test.cpp

#include "test.hpp"

const std::map<const char *, const char *> test::pinIndex = test::initializePins ();

std::map<const char *, const char *> test::initializePins () {
  std::map<const char *, const char *> pins;

  pins.insert (
    std::pair<const char *, const char *> (
      "AAAA", "BBBB"
    )
  );

  return pins;
}

const char *test::getPin (const char *pinNumber) {
  if (pinIndex.count (pinNumber) > 0) {
    return pinIndex.at (pinNumber);
  }
  else {
    printf ("Undefined pin %s!\n", pinNumber);
    exit (EXIT_FAILURE);
  }
}

3) main.cpp

#include "test.hpp"

int main () {
  test myExample;

  const char *a = myExample.getPin ("AAAA");

  exit (EXIT_SUCCESS);
}

When I compile and run it I get this error:

Undefined pin AAAA!

If I remove main.cpp and put the main function in test.cpp file, I do not get me any error and GetPin returns the correct value.

Any idea what I'm doing wrong?

Thank you

πάντα ῥεῖ

Your problem is that you're using char* pointers in your map as key values. To find entries in the map, the implementation uses comparison operations (<) for the given key.

If you're going to compare char* pointers those will almost never be the same, and are completely unrelated to their contents, which is what you're actually want to look for.

A simple solution for your problem would be to change your map type to

std::map<std::string, std::string>

Another possible solution is to provide a class that compares two char* pointers based on content as the 3rd template parameter of the map

std::map<char*, char*, MyComparer>

where MyComparer is s.th. like this

struct MyComparer {
    bool operator()( const char*& lhs, const char*& rhs ) const {
        return strcmp(lhs,rhs) < 0;
    }
};

As an explanation why you experience that seemingly inconsistent behavior, when moving your test to a separate compilation unit:
If you have the same string literals (e.g. "AAAA") appearing in the TU multiple times, the compiler can optimize them to be stored just once, and thus you'll have the same address for all of their appearances.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c++: vector of class instances, search by class member values failed

From Dev

How to map different C++ classes to enum class values

From Dev

C++ insert a value in a set of values from a class in a Map

From Dev

How to map xml attribute values to java class attributes using jaxB or any other better method?

From Dev

Impute class values based on map

From Dev

Map to method c++

From Dev

Python MyHashTable class: search method with linear probing

From Dev

C++ - Initialiser list for unordered_map of class method function pointers

From Dev

Call Java method using Map values

From Dev

Best concise method to linearize list of map values?

From Dev

Scala: duplicate execution of method in Map values

From Dev

java.util.Map values() method performance

From Dev

Search array of a class c#

From Dev

Passing custom method from custom class to .map

From Dev

QtConcurrent Map Cannot Use Class Method

From Dev

Class 'String' has no instance method 'map'

From Dev

Class 'String' has no instance method 'map'

From Dev

Class 'String' has no instance method 'map'

From Dev

Passing custom method from custom class to .map

From Dev

ReactJS call class method in Button after map()

From Dev

.containsKey() method for c++ map

From Dev

Map to method c++11

From Dev

How do you loop through a class passed to a method and retrieve it's property values in C#

From Dev

How to use extraction operator to get multiple values from method of a class in C++?

From Dev

Java get multiple values from a class method

From Dev

Clojure: search/replace values in a dynamic nested map/seq

From Dev

Convert instance method to class method C#

From Dev

C++ pointer to class method

From Dev

Objective-C Class Method

Related Related

HotTag

Archive