为什么简单的bimap(std::string<;->;int)不起作用? 一个令人困惑的问题:C++模板和类型转换如下… 为了让我的生活更轻松,我定义了一个模板类,使用类BiMap对一对一关系进行建模: #include <stdio.h> #include <stdlib.h> #include <map> #include <string> template<class T1, class T2> class BiMap { public: T2& operator[] (T1& t1) { return d1[t1]; } T1& operator[] (T2& t2) { return d2[t2]; } private: std::map<T1, T2> d1; std::map<T2, T1> d2; }; int main(int argc, char *argv[]) { BiMap<std::string, int> m; m["1"] = 2; m[2] = 3; printf("%d", m["1"]); printf("%d", m[2]); return 0; } #包括 #包括 #包括 #包括 模板 类BiMap{ 公众: T2和操作员[](T1和T1){ 返回d1[t1]; } T1和操作员[](T2和T2){ 返回d2[t2]; } 私人: std::map d1; std::map d2; }; int main(int argc,char*argv[]) { BiMap m; m[“1”]=2; m[2]=3; printf(“%d”,m[“1”); printf(“%d”,m[2]); 返回0; }

为什么简单的bimap(std::string<;->;int)不起作用? 一个令人困惑的问题:C++模板和类型转换如下… 为了让我的生活更轻松,我定义了一个模板类,使用类BiMap对一对一关系进行建模: #include <stdio.h> #include <stdlib.h> #include <map> #include <string> template<class T1, class T2> class BiMap { public: T2& operator[] (T1& t1) { return d1[t1]; } T1& operator[] (T2& t2) { return d2[t2]; } private: std::map<T1, T2> d1; std::map<T2, T1> d2; }; int main(int argc, char *argv[]) { BiMap<std::string, int> m; m["1"] = 2; m[2] = 3; printf("%d", m["1"]); printf("%d", m[2]); return 0; } #包括 #包括 #包括 #包括 模板 类BiMap{ 公众: T2和操作员[](T1和T1){ 返回d1[t1]; } T1和操作员[](T2和T2){ 返回d2[t2]; } 私人: std::map d1; std::map d2; }; int main(int argc,char*argv[]) { BiMap m; m[“1”]=2; m[2]=3; printf(“%d”,m[“1”); printf(“%d”,m[2]); 返回0; },c++,type-conversion,C++,Type Conversion,但我得到了这个编译错误: testPedigree.cpp:45: error: no match for ‘operator[]’ in ‘m["1"]’ testPedigree.cpp:16: note: candidates are: T2& BiMap<T1, T2>::operator[](T1&) [with T1 = std::basic_string<char, std::char_traits<char>, std::alloca

但我得到了这个编译错误:

testPedigree.cpp:45: error: no match for ‘operator[]’ in ‘m["1"]’
testPedigree.cpp:16: note: candidates are: T2& BiMap<T1, T2>::operator[](T1&) [with T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T2 = int]
testPedigree.cpp:19: note:                 T1& BiMap<T1, T2>::operator[](T2&) [with T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T2 = int]
testPedigree.cpp:45:错误:与'm[“1”]中的'operator[]不匹配
testPedigree.cpp:16:注:候选项为:T2&BiMap::operator[](T1&)[带T1=std::basic_字符串,T2=int]
testPedigree.cpp:19:注:T1&BiMap::operator[](T2&)[带T1=std::basic_字符串,T2=int]

我希望C++会自动将conchch*转换为STD::string,因为我在这个帖子中问了问题:

< p>你的代码试图绑定一个非->代码> const 引用到一个临时的(即,<代码> STD::String ),它是从你的代码> const char []/COD>中隐式创建的。只有
const
引用可以绑定到临时变量

尝试:

您的示例中还有其他一些小错误。下面是一个经过修正和测试的程序:

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

template<class T1, class T2>
class BiMap {
 public:
  T2& operator[] (const T1& t1) {
    return d1[t1];
  }
  T1& operator[] (const T2& t2) {
    return d2[t2];
  }
 private:
  std::map<T1, T2> d1;
  std::map<T2, T1> d2;
};


int main(int argc, char *argv[])
{

  BiMap<std::string, int> m;
  m["1"] = 2;
  m[2] = "3";

  printf("%d", m["1"]);
  printf("%s\n", m[2].c_str());
  return 0;
}
#包括
#包括
#包括
#包括
模板
类BiMap{
公众:
T2和运算符[](常数T1和T1){
返回d1[t1];
}
T1和运算符[](常数T2和T2){
返回d2[t2];
}
私人:
std::map d1;
std::map d2;
};
int main(int argc,char*argv[])
{
BiMap m;
m[“1”]=2;
m[2]=“3”;
printf(“%d”,m[“1”);
printf(“%s\n”,m[2].c_str());
返回0;
}

< /代码>也可以考虑更改<代码> PrimTf(“%d”,m(2));<代码>到
printf(“%s”,m[2].c_str())是的。也
m[2]=3似乎失败了。我不确定这是否能满足您的要求。它将前后存储结果,但您将无法查找反向结果。所以,如果你做
b[4]=“Hi”
b[“Hi”]
将不会返回
4
。不确定这是否是你想要的。
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>

template<class T1, class T2>
class BiMap {
 public:
  T2& operator[] (const T1& t1) {
    return d1[t1];
  }
  T1& operator[] (const T2& t2) {
    return d2[t2];
  }
 private:
  std::map<T1, T2> d1;
  std::map<T2, T1> d2;
};


int main(int argc, char *argv[])
{

  BiMap<std::string, int> m;
  m["1"] = 2;
  m[2] = "3";

  printf("%d", m["1"]);
  printf("%s\n", m[2].c_str());
  return 0;
}