0

这些是 g++ 发出的错误/警告,下面是相关代码。任何有关清除这些错误或阐明它们的帮助将不胜感激。谢谢!

g++ 错误:

id31.cpp: In function ‘php_var array(int, ...)’:
id31.cpp:462: warning: cannot receive objects of non-POD type ‘class php_var’ through ‘...’; call will abort at runtime
id31.cpp:480: warning: cannot receive objects of non-POD type ‘class php_var’ through ‘...’; call will abort at runtime

相关代码:

#include <stdarg.h>
php_var array(int key = 0, ...)
{
    va_list ap;
    va_start(ap, key);
    php_var arr;
    arr.to_array();
    int i = 0;
    for(int i = 0; i < key / 2; ++i)
    {
        php_var key2 = va_arg(ap, php_var);
        if(key2 == -1)
        {
            bool found = false;
            for(;;)
            {
                for(i = 0;i < arr.keys.size(); ++i)
                {
                    if(arr.keys[i] == (php_var) i)
                        found = true;
                }
                if(found)
                    ++i;
                else
                    break;
            }
            key2 = i;
        }
        php_var val = va_arg(ap, php_var);
        arr.keys.push_back(key2);
        arr.data.push_back(val);
    };
    va_end(ap);
    return arr;
}

gcc 错误:

id31.cpp: In function ‘php_var substr(php_var, php_var, php_var)’:
id31.cpp:511: error: ambiguous overload for ‘operator-’ in ‘str.php_var::container.std::basic_string<_CharT, _Traits, _Alloc>::length [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]() - pos’
id31.cpp:511: note: candidates are: operator-(size_t, int) <built-in>
id31.cpp:511: note:                 operator-(size_t, double) <built-in>
id31.cpp:511: note:                 operator-(size_t, float) <built-in>
id31.cpp:511: note:                 operator-(size_t, unsigned int) <built-in>
id31.cpp:511: note:                 operator-(size_t, long int) <built-in>

相关代码:

php_var substr(php_var str, php_var pos, php_var len)
{
    if(len == 0)
        len = str.container.length() - pos;
    return (php_var) str.container.substr(pos, len);
}
php_var substr(php_var str, long pos, long len)
{
    if(len == 0)
        len = str.container.length() - pos;
    return (php_var) str.container.substr(pos, len);
}

gcc 错误:

id31.cpp:592: error: ambiguous overload for ‘operator-’ in ‘_length - strlen(php_var)()’
id31.cpp:592: note: candidates are: operator-(const char*, const char*) <built-in>
id31.cpp:592: note:                 operator-(const char*, int) <built-in>
id31.cpp:592: note:                 operator-(const char*, int) <built-in>
id31.cpp:592: note:                 operator-(const char*, int) <built-in>
id31.cpp:592: note:                 operator-(int, int) <built-in>
id31.cpp:592: note:                 operator-(int, double) <built-in>
id31.cpp:592: note:                 operator-(int, float) <built-in>
id31.cpp:592: note:                 operator-(int, unsigned int) <built-in>
id31.cpp:592: note:                 operator-(int, long int) <built-in>
id31.cpp:592: note:                 operator-(double, int) <built-in>
id31.cpp:592: note:                 operator-(double, double) <built-in>
id31.cpp:592: note:                 operator-(double, float) <built-in>
id31.cpp:592: note:                 operator-(double, unsigned int) <built-in>
id31.cpp:592: note:                 operator-(double, long int) <built-in>
id31.cpp:592: note:                 operator-(float, int) <built-in>
id31.cpp:592: note:                 operator-(float, double) <built-in>
id31.cpp:592: note:                 operator-(float, float) <built-in>
id31.cpp:592: note:                 operator-(float, unsigned int) <built-in>
id31.cpp:592: note:                 operator-(float, long int) <built-in>
id31.cpp:592: note:                 operator-(unsigned int, int) <built-in>
id31.cpp:592: note:                 operator-(unsigned int, double) <built-in>
id31.cpp:592: note:                 operator-(unsigned int, float) <built-in>
id31.cpp:592: note:                 operator-(unsigned int, unsigned int) <built-in>
id31.cpp:592: note:                 operator-(unsigned int, long int) <built-in>
id31.cpp:592: note:                 operator-(long int, int) <built-in>
id31.cpp:592: note:                 operator-(long int, double) <built-in>
id31.cpp:592: note:                 operator-(long int, float) <built-in>
id31.cpp:592: note:                 operator-(long int, unsigned int) <built-in>
id31.cpp:592: note:                 operator-(long int, long int) <built-in>

相关代码:

php_var _recruit = _length - (php_var)strlen(_flag);
if (_recruit < (php_var)1)
{
    return _flag;

_end_18:
    return (php_var)sprintf((string)(const char*)(php_var)"%0" + (string)(const char*)_length + (string)(const char*)(php_var)"d", _flag);

}

gcc 错误:

id31.cpp:598: error: cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘char*’ for argument ‘1’ to ‘int sprintf(char*, const char*, ...)’

相关代码:

if (_recruit < (php_var)1)
{
    return _flag;

_end_18:
    return (php_var)sprintf((string)(const char*)(php_var)"%0" + (string)(const char*)_length + (string)(const char*)(php_var)"d", _flag);

}
4

3 回答 3

4
  1. 您正在传递一个php_var对象作为 variadic function 的参数php_var array(int key = 0, ...)。这是不允许的。只有 POD 对象可以作为可变参数传递。

  2. 3. a (或 的类型,不知道是什么类型)和 a之间没有operator-定义。int_lengthphp_var

  3. sprintf将 a 作为第一个参数,char*而您将 a 传递std::string给它。用于std::string::c_str()将您的字符串转换为const char*

于 2011-02-07T15:02:46.927 回答
2

第一个错误很简单,您只是不能将类型的对象php_var作为可变参数(特定于参数的函数...的参数)传递。

至于其他的错误,不看自己的定义就很难说php_var

你写的时候到底是什么意思:(string)(const char*)(php_var)"%0"?!

你取一个字符串,然后将其转换为某种未知类型,然后将其转换a const char*(它是从它开始的),然后强制它成为 a std::string(它不能被传递给printf)......为什么?

最后两个片段看起来你试图让一个参数指定 a 的长度属性printf,如果你查看它的手册页,你会看到正确的方法是这样做printf("%*d", width, num);:只需将*“宽度”选项放置在预期的位置即可告诉printf从另一个传递的参数中获取它。

最后,你所做的所有转换都是代码非常糟糕的标志,当你正确地做事时,转换应该很少发生。

于 2011-02-07T15:04:16.327 回答
2

POD =>普通旧数据:为 C 语言创建了可变参数,它们只能与不包含构造函数/析构函数的上层结构一起使用。

于 2011-02-07T15:06:00.803 回答