• 12403阅读
  • 6回复

QString类型的赋值问题(问题补充) [复制链接]

上一主题 下一主题
离线xiewenjiang
 
只看楼主 倒序阅读 楼主  发表于: 2010-04-28
定义了一结构体
struct Info_dam
    {

        std::vector<QString> current_height;
        std::vector<QString> current_layer;
        std::vector<QString> description;
        QString humidity;
        QString weather;
        QString date;
    };

然后定义了一个类并初始化
Info_dam* show_info;
show_info = new Info_dam();
当show_info中date不为空时,使用show_info->date.clear();出现堆错误。

补充一下,之前帖子的错误是没有new类导致的,当时的问题是一次赋值都不行。
现在的问题是QSring类型的指针,在同一个类里面修改,没有问题。
一旦以指针传到另一个类里,第一次赋值没有问题,第二次赋值就出现问题,出现堆错误。
[ 此帖被xiewenjiang在2010-04-29 13:52重新编辑 ]
离线steinlee

只看该作者 1楼 发表于: 2010-04-28
Write your code like
struct InfoDam
{

        std::vector<QString> current_height;
        std::vector<QString> current_layer;
        std::vector<QString> description;
        QString humidity;
        QString weather;
        QString date;
}

InfoDam * show_info = new InfoDam;
当show_info中date不为空时,使用show_info->date.clear();出现堆错误。

if there is a heap error when clear() is called, there must be a bug in Qt. I do not think it is the case.
Run valgrind on your code to see where the real problem is.

If you still can not solve the problem, do this: show_info->date = QString( "" );
simply assign an empty string to it. It does the same thing as clear();
Looking for remote C/C++ and Qt 兼职
离线yangfanxing
只看该作者 2楼 发表于: 2010-04-29
回 1楼(steinlee) 的帖子
传送:
http://www.qtcn.org/bbs/read.php?tid=27973
同名贴,已解决~
PHPWind好恶心。。。不想看这种界面。。。
离线xiewenjiang
只看该作者 3楼 发表于: 2010-04-29
Re:回 1楼(steinlee) 的帖子
引用第2楼yangfanxing于2010-04-29 11:13发表的 回 1楼(steinlee) 的帖子 :
传送:
http://www.qtcn.org/bbs/read.php?tid=27973
同名贴,已解决~



之前是忘了new,所以是一次赋值都不成功
现在的问题是二次赋值就出现问题,而且比较诡异的是在同一个类里面,QString不管怎么赋值都没有问题,一旦将一个QString类型的指针传到令一个类,第一次赋值没有问题,再次赋值就出现问题。
离线yangfanxing
只看该作者 4楼 发表于: 2010-04-29
回 3楼(xiewenjiang) 的帖子
第二次赋值是怎么弄的?
Some Code Please~
PHPWind好恶心。。。不想看这种界面。。。
离线xiewenjiang
只看该作者 5楼 发表于: 2010-05-05
Re:回 3楼(xiewenjiang) 的帖子
引用第4楼yangfanxing于2010-04-29 14:21发表的 回 3楼(xiewenjiang) 的帖子 :
第二次赋值是怎么弄的?
Some Code Please~



QString temp1 = element.toElement().attribute("date");
show_info->date = temp1;
date是QString类型
离线yangfanxing
只看该作者 6楼 发表于: 2010-05-05
回 5楼(xiewenjiang) 的帖子
show_info全局了吗?怎么叫在另一个类中?
你要保证在另一个类中的头文件也包含这样的struct定义;且该struct是传递进来的!
PHPWind好恶心。。。不想看这种界面。。。
快速回复
限100 字节
 
上一个 下一个