努力克服这 6 个g++错误

发布于 2024-10-16 12:59:00 字数 5648 浏览 4 评论 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);

}

These are the errors/warnings that g++ is putting out and below each is the relevant code. Any help with clearing these errors up or shedding some light on them would be greatly appreciated. Thank you!

g++ error:

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

relevant code:

#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 error:

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>

Relevant code:

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 error:

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>

relevant code:

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 error:

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*, ...)’

relevant code:

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左耳近心 2024-10-23 12:59:00
  1. 您正在传递一个 php_var 对象作为变量函数 php_var array(int key = 0, ...) 的参数。这是不允许的。只有 POD 对象可以作为可变参数传递。

  2. 和3. int(或者_length的类型)之间没有定义operator-,不知道它是什么类型) 和 php_var

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

  1. You're passing a php_var object as parameter for variadic function php_var array(int key = 0, ...). This is not allowed. Only POD objects can be passed as variadic parameters.

  2. and 3. There's no operator- defined between an int (or the type of _length, don't know what type it is) and a php_var.

  3. sprintf takes, as first argument, a char*, while you're passing a std::string to it. Use std::string::c_str() to convert your string to a const char*

彩扇题诗 2024-10-23 12:59:00

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

POD => Plain old data : varargs have been created for the C language, they can only be used with superstructure that not contain constructor/destructor.

も让我眼熟你 2024-10-23 12:59:00

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

至于其他错误,如果没有看到 php_var 本身的定义,很难说。

当你写: (string)(const char*)(php_var)"%0" 时,你到底想做什么?!

你获取一个字符串,然后将其转换为某种未知类型,然后将其back转换为const char*(它本来就是这样的),然后强制它成为一个std::string (无法传递给 printf)... 为什么?

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

最后,您所做的所有转换都是非常糟糕的代码的标志,当您正确执行操作时,转换应该很少发生。

The first error is simple, you just can't pass objects of type php_var as variadic parameters (parameters to functions which specific ... are the parameters).

As for the other errors, hard to say without seeing the definition of php_var itself.

What the heck are you intending when you write: (string)(const char*)(php_var)"%0" ?!

you take a string then cast it to some unknown type, then cast it back to a const char* (which it was to begin with), then force it to be a std::string (which can't be passed to printf)... why?

The last two snippets look like you are trying to let a parameter specify the length attribute of a printf, if you look at the man page for it, you'll see the proper way is to just do this: printf("%*d", width, num);. Simply placing a * where the "width" option is expected tells printf to take it from another passed parameter.

Finally, all of that casting you are doing is a sign of really bad code, when you do things correctly, casting should be a very infrequent occurrence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文