Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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++_Qt_Templates_Crash_Qvector - Fatal编程技术网

C++ 自己模板的崩溃原因

C++ 自己模板的崩溃原因,c++,qt,templates,crash,qvector,C++,Qt,Templates,Crash,Qvector,为什么我的程序在使用模板时会崩溃?我做错了什么? 这是一个测试程序,因为实际程序太大,无法发布 在这里 第一个qDebug与test1一起显示,但第二个不显示 #include <QCoreApplication> #include <QDebug> #include <QMutex> class MutexLocker { public: MutexLocker(QMutex& m) : _m(m) { _m.lock(); } ~

为什么我的程序在使用模板时会崩溃?我做错了什么? 这是一个测试程序,因为实际程序太大,无法发布 在这里 第一个
qDebug
test1
一起显示,但第二个不显示

#include <QCoreApplication>
#include <QDebug>
#include <QMutex>

class MutexLocker {
public:
    MutexLocker(QMutex& m) : _m(m) { _m.lock(); }
    ~MutexLocker() { _m.unlock(); }

private:
    QMutex& _m;
};

template<typename T>
class ThreadGuard {
public:
    ThreadGuard() { _mutex = new QMutex(); }

    ~ThreadGuard() { delete _mutex; }

    void set(const T& other) {
        MutexLocker m(*_mutex); Q_UNUSED(m);
        _r = other;
    }

    void set(int i, int j) {
        MutexLocker m(*_mutex); Q_UNUSED(m);
        _r[i] = j;
    }

    T r() const {
        MutexLocker m(*_mutex); Q_UNUSED(m);
        return _r;
    }

    const ThreadGuard<T>& operator=(const T& other) {
        set(other);
        return *this;
    }

private:
    ThreadGuard(const ThreadGuard&) {}

    T _r;
    QMutex *_mutex;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QVector<int> test1(10);

    for(int i = 0; i < 10; i++){
        test1[i] = i*2;
    }
    for(int i = 0; i < 10; i++){
        qDebug() << test1[i];
    }

    ThreadGuard<QVector<int> > test2;
    test2.r().resize(10);

    for(int i = 0; i < 10; i++){
        test2.r()[i] = i*2;
    }
    for(int i = 0; i < 10; i++){
        qDebug() << test2.r()[i];
    }


    return a.exec();
}
#包括
#包括
#包括
类互斥锁{
公众:
MutexLocker(QMutex&m):m(m){{um.lock();}
~MutexLocker(){m.unlock();}
私人:
QMutex&um;
};
模板
等级护丝板{
公众:
ThreadGuard(){u mutex=new QMutex();}
~ThreadGuard(){delete_mutex;}
无效集(常数T和其他){
互斥锁m(*_互斥);Q_未使用(m);
_r=其他;
}
无效集(int i,int j){
互斥锁m(*_互斥);Q_未使用(m);
_r[i]=j;
}
tr()常数{
互斥锁m(*_互斥);Q_未使用(m);
返回r;
}
常量ThreadGuard和运算符=(常量T和其他){
设置(其他);
归还*这个;
}
私人:
ThreadGuard(const ThreadGuard&){}
T_r,;
QMutex*_互斥体;
};
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
qvectortest1(10);
对于(int i=0;i<10;i++){
test1[i]=i*2;
}
对于(int i=0;i<10;i++){
qDebug()添加此方法:

T & r() {
    MutexLocker m(*_mutex); Q_UNUSED(m);
    return _r;
}
说明:


tr()const
返回一个
r
的副本,然后该副本将被销毁。而实际的
r
在这里不被修改
test2.r()。resize(10);
。以及以后的操作。

test2.r()返回一个副本。为什么您使用的是自实现的MutexLocker而不是提供的QMutexLocker?