Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 帮助我调试此-从';进行的转换无效;常量字符*';至';字符*';_C++_Constants - Fatal编程技术网

C++ 帮助我调试此-从';进行的转换无效;常量字符*';至';字符*';

C++ 帮助我调试此-从';进行的转换无效;常量字符*';至';字符*';,c++,constants,C++,Constants,我不明白为什么会出现这个错误 Widget.cpp: In constructor 'Widget::Widget(Generic, char*, int, int, int, QObject*)': Widget.cpp:13: error: invalid conversion from 'const char*' to 'char*' 就小部件的构造函数而言,我在任何地方都没有“const char*” class Widget: public QObject { Q_OBJEC

我不明白为什么会出现这个错误

Widget.cpp: In constructor 'Widget::Widget(Generic, char*, int, int, int, QObject*)':
Widget.cpp:13: error: invalid conversion from 'const char*' to 'char*'
就小部件的构造函数而言,我在任何地方都没有“const char*”

class Widget: public QObject {
    Q_OBJECT
    Q_PROPERTY(char *col READ getCol WRITE setCol)
    Q_PROPERTY(char *row READ getRow WRITE setRow)
    Generic visitor;
    char *_name;
    char *_widget_base;
    int _row;
    int _col;
    int _type;
    public:
    Widget(Generic visitor, char *name, int row, int col, int type, QObject *parent);
    char* widgetBase() const;
    QString getCol() const;
    void setCol(const QString &col);
    QString getRow() const;
    void setRow(const QString &row);

};

Widget::Widget(Generic v, char *name, int row, int col, int type, 
    QObject *parent = 0 ) {
    visitor = v;
    std::string str(name);
    int pos1 = str.find(":");
    int pos2 = str.rfind(":");
    _widget_base = str.substr(pos1, pos2-pos1).c_str();
    _name = name;
    _row = row;
    _col = col;
    _type = type;
}

这是一个
常量字符*

str.substr(pos1, pos2-pos1).c_str();

除了已经回答的编译器错误之外,您的代码还有一些其他问题。(至少,假设您发布的代码接近或正好是您在项目中实际使用的代码。)

即使将
\u widget\u base
更改为常量指针,这行代码也是一个问题:

_widget_base = str.substr(pos1, pos2-pos1).c_str();
substr
返回一个临时字符串对象。
c_str()
的结果仍然属于该临时字符串。并且,按照该行的写入方式,在执行该行之后,临时字符串对象将被销毁。因此,问题在于
\u widget\u base
将指向已删除的内存区域,该区域可随时重复使用

根据传递到小部件构造函数的内容,您可能在
\u name
中也会遇到类似的问题

class Widget: public QObject {
    Q_OBJECT
    Q_PROPERTY(char *col READ getCol WRITE setCol)
    Q_PROPERTY(char *row READ getRow WRITE setRow)
    Generic visitor;
    char *_name;
    char *_widget_base;
    int _row;
    int _col;
    int _type;
    public:
    Widget(Generic visitor, char *name, int row, int col, int type, QObject *parent);
    char* widgetBase() const;
    QString getCol() const;
    void setCol(const QString &col);
    QString getRow() const;
    void setRow(const QString &row);

};

Widget::Widget(Generic v, char *name, int row, int col, int type, 
    QObject *parent = 0 ) {
    visitor = v;
    std::string str(name);
    int pos1 = str.find(":");
    int pos2 = str.rfind(":");
    _widget_base = str.substr(pos1, pos2-pos1).c_str();
    _name = name;
    _row = row;
    _col = col;
    _type = type;
}
因此,您可以使用
\u widget\u base
\u name

1) 自己动态分配内存,使其不受任何其他对象的管理

std::string temp = str.substr(pos1, pos2-pos1);
_widget_base = new char[temp.length()+1];
strcpy(_widget_base, temp.c_str());
当然,您还需要在小部件的析构函数中管理该内存的删除。如果在小部件存在时可以更改此值,也可能会重新分配

2) 使这些成员变量成为字符数组而不是指针。这样,内存就成为小部件的永久部分,不需要管理。当然,您还必须确定阵列的大小

char _name[a big enough value];
char _widget_base[a big enough value];
然后:

std::string temp = str.substr(pos1, pos2-pos1);
strcpy(_widget_base, temp.c_str());
_widget_base = str.substr(pos1, pos2-pos1);
3) 使这些成员变量成为字符串对象

std::string _name;
std::string _widget_base;
然后:

std::string temp = str.substr(pos1, pos2-pos1);
strcpy(_widget_base, temp.c_str());
_widget_base = str.substr(pos1, pos2-pos1);
这种方法是最可取的,因为它最健壮,也最不容易出错。您没有可直接管理的内存,也不必担心太大而无法保存的值