【问题标题】:Class using static map does not compile使用静态映射的类无法编译
【发布时间】:2019-07-03 18:39:01
【问题描述】:

我正在 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;
}

【问题讨论】:

    标签: dictionary static undefined-reference


    【解决方案1】:

    我自己解决了这个问题。

    是否需要在 MapExample.cpp 中添加以下行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 2014-12-16
      • 1970-01-01
      • 2023-03-21
      • 2016-06-18
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多