我在linux中称为MapExample C ++类使用地图的测试。
如果只有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
什么是错误或缺失地图Example.c ++的源代码?
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;
}
我自己解决了这个问题。
有必要对MapExample.cpp行补充:
std::map <int,int> MapExample::map3;
std::map <int,SData> MapExample::map4;