C++ 编译器错误:将常量对象作为‘;这’;方法的参数丢弃限定符
我犯了这个错误 错误:将“const Polilinea”作为“int Polilinea::numeriopuntos()”的“this”参数传递会丢弃限定符[-fppermissive] 我不知道我的代码出了什么问题,这是头文件和类文件,在我的类注释中,示例中的语法与我的代码中的语法完全相同,但是当我编译它时,编译器向我抱怨这是不正确的,我的解决方案是不在方法中声明const,但我想知道是什么导致了错误。 这是头文件:C++ 编译器错误:将常量对象作为‘;这’;方法的参数丢弃限定符,c++,C++,我犯了这个错误 错误:将“const Polilinea”作为“int Polilinea::numeriopuntos()”的“this”参数传递会丢弃限定符[-fppermissive] 我不知道我的代码出了什么问题,这是头文件和类文件,在我的类注释中,示例中的语法与我的代码中的语法完全相同,但是当我编译它时,编译器向我抱怨这是不正确的,我的解决方案是不在方法中声明const,但我想知道是什么导致了错误。 这是头文件: #ifndef PUNTO #define PUNTO #ifndef
#ifndef PUNTO
#define PUNTO
#ifndef POLILINEA
#define POLILINEA
using namespace std;
class Punto{
private:
double x, y;
public:
Punto(double abcisa, double ordenada);
bool operator == (const Punto otro);
double GetX();
double GetY();
};
class Polilinea{
private:
Punto *puntos; // vector con los puntos de la linea
int num; //numero de puntos
//funciones privadas de ayuda para los metodos publicos
void ReservaMemoria(int numero_puntos);
void LiberaMemoria();
void CopiaPuntos(const Polilinea & otra);
public:
Polilinea(int numero_puntos);//constructor por defecto
Polilinea(const Polilinea & otra); //operador de copia
~Polilinea(); // destructor
//sobrecarga de operadores
Polilinea & operator = (const Polilinea & otra);
Punto operator [] (const int indice);
bool operator == (const Polilinea & otra);
bool operator != (const Polilinea & otra);
friend Polilinea operator + (const Punto sumado);
friend Polilinea operator + (const Punto sumado, const Polilinea & sumada);
//auxiliares
void SetNumPuntos(int numero);
int NumeroPuntos();
};
#endif
#endif
cpp文件:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "polilinea.h"
using namespace std;
Punto :: Punto(double abcisa = 0.0, double ordenada = 0.0): x(abcisa), y(ordenada){};
double Punto :: GetX(){
return x;
};
double Punto :: GetY(){
return y;
};
bool Punto :: operator == (Punto otro){
bool iguales;
if((x == otro.GetX()) && (y == otro.GetY()))
iguales = true;
else iguales = false;
};
Polilinea & Polilinea :: operator = (const Polilinea & otra){
if(this != &otra){
Polilinea temp(otra.NumeroPuntos());
for(int i = 0; i < num; i++)
temp[i] = otra[i];
return (*this);
};
};
Punto Polilinea :: operator [] (const int indice){
return (puntos[indice]);
};
int Polilinea :: NumeroPuntos(){
return num;
}
#包括
#包括
#包括
#包括“polilinea.h”
使用名称空间std;
Punto::Punto(double-abcisa=0.0,double-ordenada=0.0):x(abcisa),y(ordenada){};
双Punto::GetX(){
返回x;
};
双双关语::GetY(){
返回y;
};
bool Punto::operator==(Punto otro){
泥鳅;
if((x==otro.GetX())&&(y==otro.GetY())
iguales=真;
else=false;
};
Polilinea和Polilinea::运算符=(常量Polilinea和otra){
如果(此!=&otra){
Polilinea temp(otra.numeriopuntos());
for(int i=0;i
错误:将“const Polilinea”作为“int Polilinea::numeriopuntos()”的“this”参数传递会丢弃限定符[-fppermissive]
如错误所示-您正在调用NumeroPuntos
对Polilinea
的常量引用。由于numeriopuntos
不是常量函数,编译器警告您它可能会更改该值,这通常是不允许的(除非您使用-fppermission命令编译器允许)
解决此问题的最简单方法是将您的numeriopuntos
方法标记为const
:
int NumeroPuntos() const;
我将试着用简单的语言向你解释 方法
Polilinea&Polilinea::operator=
中的otra
是常量引用。这意味着:
otra
无法更改otra
上调用任何方法otra
上调用numeriopuntos
NumeroPuntos
不是常量,编译器会告诉您它不正确<代码>NumeroPuntos不会更改您的对象,但编译器“不够聪明,无法了解它”。你必须告诉他,numeriopuntos
不会改变任何事情。可以通过向方法声明中添加const来实现。像这样:
int Polilinea :: NumeroPuntos() const;
在头文件和cpp文件中
如果你这样做,一切都会好起来的。编译器将允许在otra
上调用numeriopuntos
otra
是常量,NumeroPuntos
也是常量
你应该使你所有的方法都不改变你的对象常数。这通常是正确的。TL;DR:这个问题是由一个无法再复制的问题或一个简单的印刷错误引起的。虽然类似的问题可能在这里的主题,这是一个解决的方式不太可能帮助未来的读者。这通常可以通过在发布前识别并仔细检查重现问题所需的最短程序来避免。任何其他不修改对象的函数也应该是
const
,例如`bool operator==(const Punto otro)`