2015-12-07 68 views
0

我收到此錯誤信息:傳球 '常量ChartData'「本次 'INT ChartData :: getHour()' 的說法丟棄預選賽[-fpermissive]

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(); 
                 ^

用於測試目的我刪除所有的常量。但這個錯誤依然存在。

將所有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; 

} 

回答

3

有你ChartData::getData()原型改變這應該修復它:

int getHour() const; 

你需要的是因爲at返回const reference和你打電話就可以了非const方法。

+0

THX。我得到一個解決方案。 – meLog

0

我得到一個解決方案。我無法使用QVector類中的at()方法。現在我使用[]。

實施例:

_chartDataObjects[_lastChangeChart].getWhitelist()[_lastChangeApp].calcTimes(); 
相關問題