【问题标题】:Trying to overcome these 6 g++ errors试图克服这 6 个 g++ 错误
【发布时间】:2011-02-07 14:55:01
【问题描述】:

这些是 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);

}

【问题讨论】:

  • 您能否将这些问题拆分为单独的问题?许多问题似乎无关。
  • 根本不影响答案,但您应该知道以下划线开头的名称是保留的,您不应该在代码中使用它们。
  • @Evan:这是错误的,以_[A-Z] 开头或包含__(双下划线)的名称被保留,其他都可以。
  • @Matthieu:17.4.3.2.1Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
  • @Evan:啊,但这里是全局命名空间。其他范围很好,特别是在函数、类等中......我不知道标签是如何被处理为范围的:/

标签: c++ g++ gcc-warning


【解决方案1】:
  1. 您正在传递一个 php_var 对象作为可变参数函数 php_var array(int key = 0, ...) 的参数。这是不允许的。只有 POD 对象可以作为可变参数传递。

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

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

【讨论】:

    【解决方案2】:

    POD => Plain old data : varargs 是为 C 语言创建的,它们只能与不包含构造函数/析构函数的上层结构一起使用。

    【讨论】:

      【解决方案3】:

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

      至于其他错误,不看php_var本身的定义很难说。

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

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

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

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

      【讨论】:

      • 我知道,我应该为自己尝试过而感到羞耻,但尽管如此,这是一个由 PHP 到 c++ 转换器生成的 .cpp 文件,名为 binaryPHP,可在 sourceforge 上找到。由于我是一名 PHP 程序员,我正在尝试在学习 c++ 时对其进行清理。也许我对这段似乎转换不佳的代码感到头疼。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多