使用静态地图的类无法编译

时间:2019-02-09 21:05:08

标签: dictionary static undefined-reference

我正在Linux中测试名为MapExample的C ++类中对map的使用。

如果只有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 :(得分:0)

我自己解决了这个问题。

有必要在MapExample.cpp上添加以下行:

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