C++ c++;错误:无法转换';常量标准::基本字符串<;char>';至';int';委派
我怎样才能修好它?在main函数中,当我试图使新节点具有字符串数据时,它不能。错误消息为[Error]无法将赋值中的“const std::basic_string”转换为“int”。我该怎么做C++ c++;错误:无法转换';常量标准::基本字符串<;char>';至';int';委派,c++,string,templates,C++,String,Templates,我怎样才能修好它?在main函数中,当我试图使新节点具有字符串数据时,它不能。错误消息为[Error]无法将赋值中的“const std::basic_string”转换为“int”。我该怎么做 template <typename value_type> class node : public element public : value_type data; node(); node (const value_type& T); ~
template <typename value_type>
class node : public element
public :
value_type data;
node();
node (const value_type& T);
~node();
};
template <typename value_type>
node<value_type> :: node(const value_type& T)
{
type = 0;
intd= 0;
stringd = "";
if(typeid(T)==typeid(int))
{
type= 0;intd = T;
}
else if(typeid(T)==typeid(string))
{
type = 3;stringd = T;
}
this->left = NULL;
this->right = NULL;
this->data = T;
}
int main()
{
string s1 = "123";
node *n1 = new node<string>(s1);
return 0;
}
模板
类节点:公共元素
公众:
值类型数据;
node();
节点(常数值\u类型&T);
~node();
};
模板
节点::节点(常量值\u类型&T)
{
类型=0;
intd=0;
string=“”;
if(typeid(T)=typeid(int))
{
type=0;intd=T;
}
else if(typeid(T)=typeid(字符串))
{
类型=3;字符串D=T;
}
此->左=空;
此->右=空;
这个->数据=T;
}
int main()
{
字符串s1=“123”;
节点*n1=新节点(s1);
返回0;
}
问题在于这一行:
if(typeid(T)==typeid(int))
{
type= 0;intd = T; // *** here ***
}
<>虽然您动态检查<代码>类型ID(t)=类型(int)< /> >在将<代码> t <代码>分配给<代码> int <代码>变量之前,C++是静态类型的。赋值将不会编译,因为无法将字符串
赋值给int
变量
相反,您可以使用模板专门化:
#include <string>
#include <typeinfo>
using std::string;
struct element{
int type, intd;
string stringd;
void *left, *right;
};
template <typename value_type>
class node : public element{
public :
value_type data;
node();
node (const value_type& T);
~node();
};
template <typename value_type>
node<value_type> :: node(const value_type& T)
{
type = 0;
intd= 0;
stringd = "";
this->left = NULL;
this->right = NULL;
this->data = T;
}
template <>
node<int> :: node(const int& T)
{
type= 0;
intd = T;
stringd = "";
this->left = NULL;
this->right = NULL;
this->data = T;
}
template <>
node<string> :: node(const string& T)
{
type = 3;
intd= 0;
stringd = T;
this->left = NULL;
this->right = NULL;
this->data = T;
}
// defining destructor is required to use delete
template <typename value_type>
node<value_type> :: ~node()
{
}
int main()
{
string s1 = "123";
node<string> *n1 = new node<string>(s1);
delete n1;
return 0;
}
#包括
#包括
使用std::string;
结构元素{
int类型,intd;
弦乐;
空*左,*右;
};
模板
类节点:公共元素{
公众:
值类型数据;
node();
节点(常数值\u类型&T);
~node();
};
模板
节点::节点(常量值\u类型&T)
{
类型=0;
intd=0;
string=“”;
此->左=空;
此->右=空;
这个->数据=T;
}
模板
节点::节点(常量int&T)
{
类型=0;
intd=T;
string=“”;
此->左=空;
此->右=空;
这个->数据=T;
}
模板
node::node(常量字符串&T)
{
类型=3;
intd=0;
stringd=T;
此->左=空;
此->右=空;
这个->数据=T;
}
//要使用delete,需要定义析构函数
模板
节点::~node()
{
}
int main()
{
字符串s1=“123”;
节点*n1=新节点(s1);
删除n1;
返回0;
}
我也
- 在
之后添加了类节点:public元素
{
- 将
的类型从n1
更改为node
,以避免编译错误node
节点
对象就不需要模板化,因为您将数据
声明为:
boost::variant<int, string> data;
boost::变量数据;
boost::variant
将仅处理int
和string
,但您可以添加更多模板参数来处理更多类型,例如boost::variant
如果您有兴趣了解如何处理这一难题,请查看Volker Simonis和Roland Weiss的这篇伟大的开创性文章:不要使用
typeid
,请…type=0;intd=t;
仍然需要编译,即使条件是false
。