【发布时间】:2012-09-02 02:51:15
【问题描述】:
我正在尝试使用如下模板参数声明一个 stl 映射:
(假设 T 为 typename 像这样:template <class T>)
map<T, T> m;(在.h文件中)
它编译得很好。现在在我的 cpp 文件中,当我想插入地图时,我不能。我在智能感知上获得的唯一方法是“at”和“swap”方法。
有什么想法吗?请问有人吗?
提前致谢。
这里是示例代码:
#pragma once
#include <iostream>
#include <map>
using namespace std;
template <class T>
class MySample
{
map<T, T> myMap;
//other details omitted
public:
//constructor
MySample(T t)
{
//here I am not able to use any map methods.
//for example i want to insert some elements into the map
//but the only methods I can see with Visual Studio intellisense
//are the "at" and "swap" and two other operators
//Why???
myMap.
}
//destructor
~MySample(void)
{
}
//other details omitted
};
【问题讨论】:
-
发布一些代码...我们不在您的屏幕前,所以如果您想要答案,您可能希望帮助我们了解您的问题...
-
我添加了一些示例代码。如果我做错了,请告诉我。
-
如果您只是按照我的回答中描述的行键入代码,那么代码是否可以编译?也许这只是你的 IDE 的自动建议功能的问题。
-
解析 C++ 并正确解析所有标识符的名称是异常复杂的。编写一个正确的编译器已经够难了,而且 IntelliSense 必须比完全编译更快地生成一些合理的东西,即使代码处于语法错误状态也是如此。令人惊讶的是它的效果和它一样好,但不要依赖它!这只是一种“尽力而为”的启发式方法。
标签: c++ stl map initialization std