简体   繁体   English

C ++模板函数-编译器错误:“无法从const char *更改为int”

[英]C++ template function - Compiler error: 'can not change from from const char* to int'

I have a template function specified: 我指定了一个模板函数:

template<class T> void jsonParse(json_object* jobj, eParseWhat parseWhat, T &value) {
    ...

    if(parseWhat == PARSE_UID) {
        value = json_object_getInt(val);
    }
    if(parseWhar == PARSE_EMAIL) {
        value = json_object_getString(val);
    }
    ...
}

Now when I want to parse the uid of the json-object I call the method with an int : 现在,当我想解析json对象的uid ,我用一个int调用该方法:

json_object* obj = ...;
int uid = 0;
jsonParse(obj,PARSE_UID,uid);

But then a compile error occurs at the assignment in line: 但是然后在行中的分配处发生编译错误:

value = json_object_getString(val);

Because of the call with an int the compiler thinks that the type of the variable value is int and json_object_getString(val) returns const char* . 由于使用int进行调用,因此编译器认为变量value的类型为int并且json_object_getString(val)返回const char* So the compiler says: can not convert from const char* to int . 所以编译器说: can not convert from const char* to int

Have you got any suggestion to solve this problem? 您有解决此问题的建议吗?

Why are you even using a template if you are going to do a switch statement of a bunch of if statements for each type? 如果要对每种类型执行一系列if语句的switch语句,为什么还要使用模板? Your template instantiation will never compile if you want to treat every type as multiple types. 如果要将每种类型都视为多个类型,则模板实例化将永远不会编译。 This design is flawed but if you must then you can use specializations to accomplish something similar. 这种设计是有缺陷的,但是如果您必须这样做,则可以使用专业知识来完成类似的工作。

template<class T> void jsonParse(json_object* jobj, eParseWhat parseWhat, T &value) {
    static_assert(false); // not a handled type
}

template<> void jsonParse(json_object* jobj, eParseWhat parseWhat, int &value) {
    value = json_object_getInt(val);
}

template<> void jsonParse(json_object* jobj, eParseWhat parseWhat, std::string &value) {
    value = json_object_getString(val);
}

As GMan points out it is usually preferable to overload functions rather than specialize a function template. 正如GMan所指出的那样,通常最好重载函数,而不是专门化函数模板。 The equivalent using an overloaded function would be something like this: 使用重载函数的等效项将是这样的:

void jsonParse(json_object* jobj, eParseWhat parseWhat, int &value) {
        value = json_object_getInt(val);
    }

void jsonParse(json_object* jobj, eParseWhat parseWhat, std::string &value) {
        value = json_object_getString(val);
    }

Didn't you intend to write: 您不是要写:

    if(parseWhat == PARSE_UID) {
        value = json_object_getInt(val);
    }
    else if(parseWhar == PARSE_EMAIL) {
        value = json_object_getString(val);
    }

On a sidenote: template usage in your example is quite strange and confusing to me. 附带说明:您示例中的模板用法对我来说很奇怪,令人困惑。 I'd rather use some kind of Variant class for this (one example is boost::any , but you can easily implement your own - it's a matter of two hours). 我宁愿为此使用某种Variant类(一个示例是boost :: any ,但是您可以轻松实现自己的-两个小时的事情)。

Your code won't compile in any way, cause in that method T value can't be int and char* at the same time... 您的代码将不会以任何方式进行编译,因为该方法中的T值不能同时为int和char *。

You try to cast differnt types (one is a int, on is a char) to a value (that isn't even defined) ;) 您尝试将不同类型(一个是int,一个on是一个char)转换为一个值(甚至没有定义);)

First you have to use: 首先,您必须使用:

json_object* obj = ...;
int uid = 0;

jsonParse<int>(obj,PARSE_UID,uid); //last parameter is int

or 要么

jsonParse<string>(obj,PARSE_UID,whatever); //last parameter is string

But that don't work, as said before... you can't use a type in two diffrent ways in one template... 但这不起作用,如前所述...您不能在一个模板中以两种不同的方式使用类型...

Sorry... :) 对不起... :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++错误:从&#39;int&#39;到&#39;const char *&#39;的无效转换[-fpermissive] | - C++ error: invalid conversion from 'int' to 'const char*' [-fpermissive]| C ++ - 错误C2664:'int scanf(const char *,...)':无法将参数1从'int'转换为'const char *' - C++ — error C2664: 'int scanf(const char *,…)' : cannot convert argument 1 from 'int' to 'const char *' C ++模板将const char数组转换为int - C++ Template const char array to int 如何从 const char* 转换为 unsigned int c++ - How to convert from const char* to unsigned int c++ C++ 错误:从“const char*”到“int”的无效对话 - C++ eror:invalid conversation from 'const char*' to 'int' 从&#39;int&#39;到&#39;const char *&#39;的无效转换问题C ++ - Invalid conversion from 'int' to 'const char*' issue C++ 如何解决“错误:在C ++中从&#39;int&#39;到&#39;const char *&#39;[-fpermissive]&#39;的无效转换? - How to fix 'error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]' in C++? (C++) 错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive] - (C++) error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] C ++错误:从&#39;char&#39;到&#39;const char *&#39;的无效转换[-fpermissive] - C++ error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] c ++错误:从不兼容的类型'const char *'分配'char *' - c++ error: assigning to 'char *' from incompatible type 'const char *'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM