Класс с использованием статической карты не компилируется - PullRequest
0 голосов
/ 10 февраля 2019

Я тестирую в linux использование map в классе C ++ с именем MapExample.

Если в этом классе присутствуют только map1 и map2, он компилируется и работает правильно, показывая на экране выход:

Hello from the MapExample constructor.
Size of map1 is 4.
1  1
2  1
3  1
4  1
key 3 is in the map.
key 7 is not in the map.


Size of map2 is 4.
1  5  abcd
2  5  abcd
3  5  abcd
4  5  abcd
key 3 is in the map.
key 7 is not in the map.

Однако, если я добавлю map3 и map4, которые идентичны map1 и map2, но имеют статический тип и вызываются из статических методов при компиляции (g ++ -Wall -g MapExample.cpp-o mapExample) Я получаю следующие ошибки:

/tmp/ccBplL3c.o: In function `MapExample::UseMap3()':
/home/my_name/map_static/MapExample.cpp:69: undefined reference to `MapExample::map3'
/home/my_name/map_static/MapExample.cpp:71: undefined reference to `MapExample::map3'
/tmp/ccBplL3c.o: In function `MapExample::UseMap4()':
/home/my_name/map_static/MapExample.cpp:86: undefined reference to `MapExample::map4'
/home/my_name/map_static/MapExample.cpp:89: undefined reference to `MapExample::map4'
collect2: error: ld returned 1 exit status

Что неправильно или отсутствует в исходном коде MapExample.cpp?

MapExample.h

#ifndef _MAP_EXAMPLE_
#define _MAP_EXAMPLE_


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <iterator>
#include <iostream>


struct SData
{
  int    strSize;
  char * strPtr;
};



class MapExample 
{
private:
  std::map <int,int> map1;
  std::map <int,SData> map2;

  static std::map <int,int> map3;
  static std::map <int,SData> map4;

public:
  MapExample();

  void UseMap1();
  void UseMap2();

  static void UseMap3();
  static void UseMap4();

};


#endif

MapExample.cpp

#include "MapExample.h"


MapExample::MapExample()
{
  printf("Hello from the MapExample constructor.\n");
}


void MapExample::UseMap1()
{
  for (int aux=1; aux<5; aux++)
    map1[aux]++;

  printf("Size of map1 is %d.\n", (int)(map1.size()));

  std::map<int,int>::iterator iter;
  for (iter = map1.begin(); iter != map1.end(); ++iter)
    printf("%d  %d\n", iter->first, iter->second);

  if (map1.find(3) != map1.end())
    printf("key 3 is in the map.\n");
  else
    printf("key 3 is not in the map.\n");

  if (map1.find(7) != map1.end())
    printf("key 7 is in the map.\n");
  else
    printf("key 7 is not in the map.\n");
}


void MapExample::UseMap2()
{
  SData sdat;

  for (int aux=1; aux<5; aux++)
  {
    sdat.strSize=5;
    sdat.strPtr = new char[5];
    memset(sdat.strPtr,0,5);
    strcpy(sdat.strPtr, "abcd");

    map2[aux] = sdat;
  }

  printf("Size of map2 is %d.\n", (int)(map2.size()));

  std::map <int,SData>::iterator iter;
  for (iter = map2.begin(); iter != map2.end(); ++iter)
    printf("%d  %d  %s\n", iter->first, iter->second.strSize, iter->second.strPtr);

  if (map2.find(3) != map2.end())
    printf("key 3 is in the map.\n");
  else
    printf("key 3 is not in the map.\n");

  if (map2.find(7) != map2.end())
    printf("key 7 is in the map.\n");
  else
    printf("key 7 is not in the map.\n");
}



void MapExample::UseMap3()
{
  for (int aux=1; aux<5; aux++)
    MapExample::map3[aux]++;

  printf("Size of map3 is %d.\n", (int)(MapExample::map3.size()));
}


void MapExample::UseMap4()
{
  SData sdat;

  for (int aux=1; aux<5; aux++)
  {
    sdat.strSize=5;
    sdat.strPtr = new char[5];
    memset(sdat.strPtr,0,5);
    strcpy(sdat.strPtr, "abcd");

    MapExample::map4[aux] = sdat;
  }

  printf("Size of map4 is %d.\n", (int)(map4.size()));
}


int main()
{
  MapExample MapObj;

  MapObj.UseMap1();
  printf("\n\n");
  MapObj.UseMap2();

  printf("\n\n");
  MapObj.UseMap3();
  printf("\n\n");
  MapObj.UseMap4();

  return 0;
}

1 Ответ

0 голосов
/ 10 февраля 2019

Я решил эту проблему самостоятельно.

Необходимо добавить на MapExample.cpp строки:

std::map <int,int> MapExample::map3;
std::map <int,SData> MapExample::map4;
...