传递'const ChartData'作为'这个' ' int ChartData :: getHour()'的参数丢弃限定符[-fpermissive]

时间:2015-12-07 09:30:34

标签: c++ qt const

我收到此错误消息:

C:\Users\Rick\Documents\Qt-Übung\testVisualizer\main.cpp:12: error: passing 'const ChartData' as 'this' argument of 'int ChartData::getHour()' discards qualifiers [-fpermissive]
     qDebug() << myVis.getChartDataVector().at(0).getHour();
                                                          ^

出于测试目的,我删除所有const。但是这个错误仍然存​​在。

将所有getter作为const方法我每次调用setter时都会遇到60多个错误。

我不知道它来自哪里。

的main.cpp

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Visualize myVis;
    myVis.handleDataFromString("C:\\Users\\Rick\\Google Drive\\FH Sachen\\5. Semester\\EMS\\ricks code\\maitime-20151026.json");
    qDebug() << myVis.getChartDataVector().at(0).getHour();
    /*
    for(int i = 0; i < myVis.getChartDataVector().size(); i++){
        qDebug() << "--------Whitelist--------";
        for(int j = 0; j < myVis.getChartDataVector().at(i).getWhitelist().size(); j++){
            qDebug() << "Stunde: " << myVis.getChartDataVector().at(i).getHour() << "Name: " << myVis.getChartDataVector().at(i).getWhitelist().at(j).getName() << "Zeit: " << myVis.getChartDataVector().at(i).getWhitelist().at(j).getTotalTime();
        }
        qDebug() << "--------Blacklist--------";
        for(int j = 0; myVis.getChartDataVector().at(i).getBlacklist().size(); j++){
            qDebug() << "Stunde: " << myVis.getChartDataVector().at(i).getHour() << "Name: " << myVis.getChartDataVector().at(i).getBlacklist().at(j).getName() << "Zeit: " << myVis.getChartDataVector().at(i).getBlacklist().at(j).getTotalTime();
        }
    }
    */
    return a.exec();
}

visualize.h

#ifndef VISUALIZE_H
#define VISUALIZE_H

#include <QObject>
#include <QVector>
#include "applicationdata.h"
#include "chartdata.h"

class Visualize
{
QVector<ChartData> _chartDataObjects;

int _lastChangeChart, _lastChangeApp, _chartRef, _appRef;
bool _lastChange;


public:
    Visualize();

    QVector<ChartData> &getChartDataVector();

    void handleDataFromString(QString filepath);

private:
    void findPosition(QString name, bool onWhitelist, int hour, int &chartRef, int &appRef, QString url = "");


signals:

public slots:
};

#endif // VISUALIZE_H

visualize.cpp

#include "visualize.h"
#include "applicationdata.h"
#include "chartdata.h"

#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>


long ApplicationData::_totalTimeAll(0);

Visualize::Visualize()
{

}

QVector<ChartData> &Visualize::getChartDataVector()
{
    return _chartDataObjects;
}

void Visualize::handleDataFromString(QString filepath)
{
    QString line;
    QFile file(filepath);
    QJsonDocument d;
    QJsonObject o;

    ChartData tempChart;
    ApplicationData tempApp;

    QString tempName, tempUrl, tempTimeString;
    bool tempOnWhitelist;
    QTime tempTime, fullHour;

    int tempHour;

    _lastChangeChart = -1, _lastChangeApp = -1, _chartRef = -1, _appRef = -1;

    file.open(QIODevice::ReadOnly | QIODevice::Text);

    if(!file.exists()){
        qDebug() << "Error read File!";
    }

    while(!file.atEnd()){

        line = file.readLine();
        d = QJsonDocument::fromJson(line.toUtf8());
        o = d.object();

        tempName = o.value(QString("process")).toString();
        tempUrl = o.value(QString("url")).toString();
        tempTimeString = o.value(QString("time")).toString();
        tempOnWhitelist = o.value(QString("whitelisted")).toBool();
        tempTime = QTime::fromString(tempTimeString, "hh:mm:ss");
        tempHour = tempTime.hour();

        //existing object is searched in vector
        findPosition(tempName, tempOnWhitelist, tempHour, _chartRef, _appRef, tempUrl);

        //if instance with actual hour not exist
        if(_chartRef == -1){
            tempChart.setHour(tempHour);
            _chartDataObjects.append(tempChart);
            _chartRef = _chartDataObjects.size()-1;
            //_lastChangeChart = _chartRef;
        }

        //if an old object exist
        if(_lastChangeApp != -1){
            if(_chartDataObjects[_lastChangeChart].getHour() == tempHour){
                if(_lastChange){
                    _chartDataObjects[_lastChangeChart].getWhitelist().at(_lastChangeApp).setEndTime(tempTime);
                    _chartDataObjects[_lastChangeChart].getWhitelist().at(_lastChangeApp).calcTimes();
                } else {
                    _chartDataObjects[_lastChangeChart].getBlacklist().at(_lastChangeApp).setEndTime(tempTime);
                    _chartDataObjects[_lastChangeChart].getBlacklist().at(_lastChangeApp).calcTimes();
                }
            } else {
                if(_lastChange){
                    fullHour.setHMS(tempHour, 0, 0);
                    _chartDataObjects[_lastChangeChart].getWhitelist().at(_lastChangeApp).setEndTime(fullHour);
                    _chartDataObjects[_lastChangeChart].getWhitelist().at(_lastChangeApp).calcTimes();
                    tempApp.setName(_chartDataObjects[_lastChangeChart].getWhitelist().at(_lastChangeApp).getName());
                    tempApp.setUrl(_chartDataObjects[_lastChangeChart].getWhitelist().at(_lastChangeApp).getUrl());
                    tempApp.setStartTime(fullHour);
                    tempApp.setEndTime(tempTime);
                    _chartDataObjects[_chartRef].getWhitelist().append(tempApp);
                    _chartDataObjects[_chartRef].getWhitelist().at(_chartDataObjects[_chartRef].getWhitelist().size()-1).calcTimes();
                } else {
                    fullHour.setHMS(tempHour, 0, 0);
                    _chartDataObjects[_lastChangeChart].getBlacklist().at(_lastChangeApp).setEndTime(fullHour);
                    _chartDataObjects[_lastChangeChart].getBlacklist().at(_lastChangeApp).calcTimes();
                    tempApp.setName(_chartDataObjects[_lastChangeChart].getBlacklist().at(_lastChangeApp).getName());
                    tempApp.setUrl(_chartDataObjects[_lastChangeChart].getBlacklist().at(_lastChangeApp).getUrl());
                    tempApp.setStartTime(fullHour);
                    tempApp.setEndTime(tempTime);
                    _chartDataObjects[_chartRef].getBlacklist().append(tempApp);
                    _chartDataObjects[_chartRef].getBlacklist().at(_chartDataObjects[_chartRef].getBlacklist().size()-1).calcTimes();
                }
            }
        }

        if(tempName == "stopped"){
            _lastChangeApp = -1;
            _lastChangeChart = -1;
            qDebug() << "Log gestoppet";
        } else if(_appRef != -1){
            if(tempOnWhitelist){
                _chartDataObjects[_chartRef].getWhitelist().at(_appRef).setStartTime(tempTime);
                _lastChangeApp = _appRef;
            } else {
                _chartDataObjects[_chartRef].getBlacklist().at(_appRef).setStartTime(tempTime);
                _lastChangeApp = _appRef;
            }
        } else {
            tempApp.setName(tempName);
            tempApp.setStartTime(tempTime);
            tempApp.setUrl(tempUrl);
            if(tempOnWhitelist){
                _chartDataObjects[_chartRef].getWhitelist().append(tempApp);
                _lastChangeApp = _chartDataObjects[_chartRef].getWhitelist().size()-1;
                _lastChange = true;
            } else {
                _chartDataObjects[_chartRef].getBlacklist().append(tempApp);
                _lastChangeApp = _chartDataObjects[_chartRef].getBlacklist().size()-1;
                _lastChange = false;
            }
        }
    }
    file.close();
}

void Visualize::findPosition(QString name, bool onWhitelist, int hour, int &chartRef, int &appRef, QString url)
{
    if(onWhitelist){
        for(int i = 0; i < _chartDataObjects.size(); i++){
            if(hour == _chartDataObjects[i].getHour()){
                for(int j = 0; j < _chartDataObjects[i].getWhitelist().size(); j++){
                    if(name  == _chartDataObjects[i].getWhitelist().at(j).getName() && url == _chartDataObjects[i].getWhitelist().at(j).getUrl()){
                        chartRef = i;
                        appRef = j;
                    } else {
                        chartRef = i;
                        appRef = -1;
                    }
                }
            } else {
                chartRef = -1;
                appRef = -1;
            }
        }
    } else {
        for(int i = 0; i < _chartDataObjects.size(); i++){
            if(hour == _chartDataObjects[i].getHour()){
                for(int j = 0; j < _chartDataObjects[i].getBlacklist().size(); j++){
                    if(name  == _chartDataObjects[i].getBlacklist().at(j).getName() && url == _chartDataObjects[i].getBlacklist().at(j).getUrl()){
                        chartRef = i;
                        appRef = j;
                    } else {
                        chartRef = i;
                        appRef = -1;
                    }
                }
            } else {
                chartRef = -1;
                appRef = -1;
            }
        }
    }
}

chartdata.h

#ifndef CHARTDATA_H
#define CHARTDATA_H

#include "applicationdata.h"


class ChartData
{
    int _hour;
    QVector<ApplicationData> _whitelist;
    QVector<ApplicationData> _blacklist;

public:
    ChartData();

    void setHour(int hour);
    int getHour();

    QVector<ApplicationData> &getWhitelist();
    QVector<ApplicationData> &getBlacklist();


signals:

public slots:
};

#endif // CHARTDATA_H

chartdata.cpp

#include "chartdata.h"

ChartData::ChartData()
{

}

void ChartData::setHour(int hour)
{
    _hour = hour;
}


int ChartData::getHour()
{
    return _hour;
}

QVector<ApplicationData> &ChartData::getWhitelist()
{
    return _whitelist;
}

QVector<ApplicationData> &ChartData::getBlacklist()
{
    return _blacklist;
}

chartdata.cpp

#include "chartdata.h"

ChartData::ChartData()
{

}

void ChartData::setHour(int hour)
{
    _hour = hour;
}


int ChartData::getHour()
{
    return _hour;
}

QVector<ApplicationData> &ChartData::getWhitelist()
{
    return _whitelist;
}

QVector<ApplicationData> &ChartData::getBlacklist()
{
    return _blacklist;
}

applicationdata.h

#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H


#include <QObject>
#include <QString>
#include <QTime>
#include <QDebug>
#include <QVector>


class ApplicationData
{
QString _name, _url;
QTime _startTime, _endTime;
long _totalTime;

static long _totalTimeAll;



public:
    ApplicationData(QString name = "", QString url = ""):
                    _name(name), _url(url), _startTime(0,0), _endTime(0,0), _totalTime(0){}

    //Setter
    void setName(QString name);
    void setUrl(QString url);
    void setStartTime(QTime start);
    void setEndTime(QTime end);
    void setTotalTime(long totalTime);

    //Getter
    QString getName();
    QString getUrl();
    QTime getStartTime();
    QTime getEndTime();
    long getTotalTime();
    static long getTotalTimeAll();

    //Methodes
    void calcTimes();

signals:

public slots:

};

#endif // APPLICATIONDATA_H

applicationdata.cpp

#include "applicationdata.h"


//Setter-------------------------------------------------------------------------
void ApplicationData::setName(QString name)
{
    _name = name;
}

void ApplicationData::setUrl(QString url)
{
    _url = url;
}

void ApplicationData::setStartTime(QTime start)
{
    _startTime = start;
}

void ApplicationData::setEndTime(QTime end)
{
    _endTime = end;
}


void ApplicationData::setTotalTime(long totalTime)
{
    _totalTime = totalTime;
}


//Getter--------------------------------------------------------------------------
QString ApplicationData::getName()
{
    return _name;
}

QString ApplicationData::getUrl()
{
    return _url;
}

QTime ApplicationData::getStartTime()
{
    return _startTime;
}

QTime ApplicationData::getEndTime()
{
    return _endTime;
}


long ApplicationData::getTotalTime()
{
    return _totalTime;
}

long ApplicationData::getTotalTimeAll()
{
    return _totalTimeAll;
}

//Methoden--------------------------------------------------------------------------
void ApplicationData::calcTimes()
{
    long tempTime;

    tempTime = _startTime.secsTo(_endTime);

    _totalTime += tempTime;
    _totalTimeAll += tempTime;

}

2 个答案:

答案 0 :(得分:3)

NoSuchElement原型更改为此应修复它:

ChartData::getData()

您需要这样做,因为 int getHour() const; 会返回const reference并且您正在调用非const方法。

答案 1 :(得分:0)

我得到一个解决方案。我无法使用QVector类中的at()方法。现在我使用[]。

示例:

_chartDataObjects[_lastChangeChart].getWhitelist()[_lastChangeApp].calcTimes();