Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;错误:无法转换';常量标准::基本字符串<;char>';至';int';委派_C++_String_Templates - Fatal编程技术网

C++ c++;错误:无法转换';常量标准::基本字符串<;char>';至';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); ~

我怎样才能修好它?在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);
    ~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
    ,以避免编译错误

您最终会希望将节点对象放入某种容器(如链表)中。容器不处理不同类型的元素。您可以在容器中存储void*指针以隐藏其内部详细信息。但随后您将放弃编译器类型检查

返回类型检查的一种方法是隐藏在一个类(如boost::variant)中保存多个类型的机制。这样,您的
节点
对象就不需要模板化,因为您将
数据
声明为:

boost::variant<int, string> data;
boost::变量数据;
boost::variant
将仅处理
int
string
,但您可以添加更多模板参数来处理更多类型,例如
boost::variant


如果您有兴趣了解如何处理这一难题,请查看Volker Simonis和Roland Weiss的这篇伟大的开创性文章:

不要使用
typeid
,请…
type=0;intd=t;
仍然需要编译,即使条件是
false