gpt4 book ai didi

c++ - 将 'const ChartData' 作为 'this' 的 'int ChartData::getHour()' 参数传递会丢弃限定符 [-fpermissive]

转载 作者:行者123 更新时间:2023-11-28 00:08:42 25 4
gpt4 key购买 nike

我收到此错误消息:

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

可视化.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

可视化.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;
}
}
}
}

图表数据.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;
}

图表数据.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;
}

应用程序数据.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

应用程序数据.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;

}

最佳答案

像这样更改您的 ChartData::getData() 原型(prototype)应该可以修复它:

 int getHour() const;

你需要它,因为 at 返回一个 const reference并且您在其上调用非常量方法。

关于c++ - 将 'const ChartData' 作为 'this' 的 'int ChartData::getHour()' 参数传递会丢弃限定符 [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34130272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com