如何在GCC编译中修复const char *构造函数转换链错误 [英] How do I fix a const char * constructor conversion chain error in GCC compile

查看:493
本文介绍了如何在GCC编译中修复const char *构造函数转换链错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试编译下面的iOS4 sdk版本的gcc。

它给我的错误:



从'const char [4]'到非标量类型'UsesStr'请求

  class strptr {
public:
strptr(const char * s):s_(s){}
const char * c_str()const {return(s_); }
protected:
const char * s_;
};


class UsesStr {
public:
UsesStr(const strptr& sp)
:p_(sp.c_str())
{
}
const char * p_;
};


static UsesStr ustr =xxx;

这是一个简单的情况,这是一个问题是当strptr是一个字符串类,但是错误是一样的。






基于下面的答案我试过这似乎工作。
想要有一个通用字符串参数,它将接受许多类型的字符串,因为它将转换放在构造函数中去除所有的转换,而不需要完全声明所有可能的字符串类型的东西只使用一种类型。

  class UsesStr; 

class strptr {
public:
strptr(const char * s):s_(s){}
strptr(UsesStr& s);

const char * c_str()const {return(s_); }
operator const char *()const {return(s_); }

private:
const char * s_;
};


class UsesStr {
public:
template< typename arg>
UsesStr(const arg& sp)
:p_(strptr(sp).c_str())
{}
UsesStr(const strptr& sp):p_ .c_str())
{}
const char * p_;
operator const strptr()const {return(strptr(p_)); }
};

strptr :: strptr(UsesStr& s)
:s_(s.p_){}


static UsesStr ustr =xxx ;
static UsesStr ustr2 = ustr;
static strptr sp = ustr2;
static UsesStr ustr3 = sp;


解决方案

  static UsesStr ustr =xxx; 

需要两个隐式转换,第一个从 const char [4] code>到 strptr ,第二个从 strptr UsesStr 。您不能在一行中有两个隐式用户转换。这些将工作:

  static UsesStr ustr = strptr(xxx); 
static UsesStr ustr = UsesStr(xxx);
static UsesStr ustr(xxx);

如果你真的需要有你编写的代码,那么你需要添加 UsesStr 来自 strptr 的构造函数。


If I try to compile the below with the iOS4 sdk version of gcc.
It gives me the error:

Conversion from 'const char[4]' to non-scalar type 'UsesStr' requested

class strptr {
public:
    strptr(const char * s) : s_(s) {}
    const char *c_str() const { return(s_); }
protected:
    const char *s_;
};


class UsesStr {
public:
    UsesStr(const strptr &sp)
        : p_(sp.c_str())
    {
    }
    const char *p_;
};


static UsesStr ustr = "xxx";

This is a simple case, it is a problem is when strptr is a string class that is used instead but the error is the same.


based on answer below I tried this which does seem to work. Desire to have a "universal" string arg that will accept many types of strings as it puts the conversion in the constructor to factor out all the conversions and not require complete declarations of all the possible string types in things that use only one type.

class UsesStr;

class strptr {
public:
    strptr(const char * s) : s_(s) {}
    strptr(UsesStr &s);

    const char *c_str() const { return(s_); }
    operator const char *() const { return(s_); }

private:
    const char *s_;
};


class UsesStr {
public:
    template<typename arg>
    UsesStr(const arg &sp)
        : p_(strptr(sp).c_str())
    {}
    UsesStr(const strptr &sp) : p_(sp.c_str()) 
    {}
    const char *p_;
    operator const strptr() const { return(strptr(p_)); } 
};

strptr::strptr(UsesStr &s)
    : s_(s.p_) {}


static UsesStr ustr = "xxx";
static UsesStr ustr2 = ustr;
static strptr sp = ustr2;    
static UsesStr ustr3 = sp;

解决方案

static UsesStr ustr = "xxx";

requires two implicit conversions, the first from const char[4] to strptr and the second from strptr to UsesStr. You can't have two implicit user convertions in a row. These would work:

static UsesStr ustr = strptr( "xxx" );
static UsesStr ustr = UsesStr( "xxx" );
static UsesStr ustr( "xxx" );

If you really need to have the code as you wrote it, then you will need to add in UsesStr a constructor from a strptr.

这篇关于如何在GCC编译中修复const char *构造函数转换链错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆