大家好我接受了一个我无法理解的错误。在我的主要类中,void GPSPathList :: print()const 我收到了这个错误。
[Error] passing 'const GPSPathList' as 'this' argument of 'void GPSPathList::print_item(const GPSPathList::GPSCoordNode*)' discards qualifiers [-fpermissive]
你能帮忙吗
MY标题
#ifndef GPSPATHLIST_H
#define GPSPATHLIST_H
class GPSPathList {
public:
GPSPathList(){ m_size = 0; m_item_list = 0; }
int size() const{return m_size;}
void add_from_user();
void print() const;
double total_distance();
private:
struct GPSCoordNode{
double longtitude;
double lattitude;
GPSCoordNode *next;
};
void print_item(const GPSCoordNode *item);
void addpoint(const double& lattitude, const double& longtitude);
int m_size;
GPSCoordNode *m_item_list;
};
#endif
我的主要
#include "gpspathlist.h"
#include <iostream>
#include <cmath>
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
using std::string;
void GPSPathList::print_item(const GPSCoordNode *item){
cout<< "<"<<item->lattitude<<","<<item->longtitude<<">"<<endl;
}
void GPSPathList::addpoint(const double& lattitude , const double& longtitude){
GPSCoordNode *new_point = new GPSCoordNode;
new_point->lattitude = lattitude;
new_point->longtitude = longtitude;
if (m_item_list == 0 ) {
new_point->next = m_item_list;
m_item_list = new_point;
}
else {
GPSCoordNode *p = m_item_list;
while (p->next != 0)
p = p->next;
new_point->next = p->next;
p->next = new_point;
}
m_size++;
}
void GPSPathList::add_from_user(){
string a;
double lattitude,longtitude;
while(true){
cout << "Enter lattıtude : " <<endl;
cin >> lattitude;
cout << "Enter longtıtude : " <<endl;
cin >> longtitude;
addpoint(lattitude,longtitude) ;
cout <<"if exit press no to ";
cin >>a;
if(a=="no"){
break;
}
}
}
void GPSPathList::print() const
{
const GPSCoordNode *d = m_item_list;
while (d != 0) {
print_item(d);//my problem is in here
d=d->next;
}
}
double GPSPathList::total_distance(){
double a,b,c,d,longtitude,lattitude,longtitude2,lattitude2,R,S;
GPSCoordNode *v = m_item_list;
for (int i =0; i<m_size; i++){
a=v->lattitude;
b=v->longtitude;
v=v->next;
c=v->lattitude;
d=v->longtitude;
v=v->next;
i++;
R = 6371.8;
longtitude= (3.141593/180.0) * b;
lattitude = (3.141593/180.0) * a;
longtitude2=(3.141593/180.0) * d;
lattitude2=(3.141593/180.0) * c;
double Lat =lattitude2-lattitude;
double Lon = longtitude2-longtitude;
double w = sin(Lat / 2);
double x=sin(Lon / 2);
double q= R * asin(sqrt((w*w)+(cos(lattitude) * cos(lattitude2)*x*x)));
i++;
return S=q*1.609;
cout<<" distance: "<< S;
}
}
int main(int argc, char** argv){
GPSPathList a;
a.add_from_user();
a.print();
a.size();
a.total_distance();
}
大家好我接受了一个我无法理解的错误。在我的主要类中,void GPSPathList :: print()const 我收到了这个错误。
答案 0 :(得分:1)
您无法从const
个方法调用非const
方法。考虑将print_item
作为const
方法。