la valeur initiale de la référence à non-const doit être une lvalue, Passer un type d'objet par erreur de référence

Pawan Nirpal

J'obtiens l'erreur suivante lorsque j'essaie de passer un Pointobjet par référence en gardant Pointles variables membres x et y comme privées, c'est pourquoi j'obtiens x et y par fonction GetX()et GetY()comment puis-je résoudre l'erreur et la faire fonctionner comme je l'attends pour.

journal des erreurs :

CppReview.cpp: In function 'int main()':
CppReview.cpp:92:30: error: cannot bind non-const lvalue reference of type 'Point&' to an rvalue of type 'Point'
   92 |     v.OffSetVector(v.GetStart(),1,3);
      |                    ~~~~~~~~~~^~
CppReview.cpp:79:34: note:   initializing argument 1 of 'void Vector::OffSetVector(Point&, int, int)'
   79 | void Vector::OffSetVector(Point& p,int xoffset,int yoffset){

code :


class Point{
    private:
        int x,y;
    public:
        Point(){
            x = y = 0;
        }
        Point(int x,int y){
            this->x = x;
            this->y = y;
        }
        Point(float x,float y){
            this->x = x;
            this->y = y;
        }
        void SetX(int x){
            this->x = x;
        }
        
        void SetY(int y){
            this->y = y;
        }
        void Display(){
            cout<<"("<<this->x<<','<<this->y<<")\n";
        }
        void move(int i=0,int j=0){
            this->x+=i;
            this->y+=j;
        }

        int& GetX(){
            return (this->x);
        }

        int& GetY(){
            return this->y;
        }
};

class Vector{
    Point start,end;
    public:
    Vector(){
        this->start = Point(0,0);
        this->end = Point(1,1);
    }
    Vector(int sx,int sy,int ex,int ey){
        this->start = Point(sx,sy);
        this->end = Point(ex,ey);
    }
    float ComputeDistance(Point,Point);
    Point GetStart();
    Point GetEnd();
    void OffSetVector(Point&,int,int);
    void Show();
};

float Vector::ComputeDistance(Point p1,Point p2){
    int p1x = p1.GetX();
    int p1y = p1.GetY();
    int p2x = p2.GetX();
    int p2y = p2.GetY();
    float dist = sqrt((p1x-p2x)*(p1x-p2x)+(p1y-p2y)*(p1y-p2y));
    return dist;
}

Point Vector::GetStart(){
    return this->start;
}

Point Vector::GetEnd(){
    return this->end;
}

void Vector::OffSetVector(Point& p,int xoffset,int yoffset){
    p.GetX()+=xoffset;
    p.GetY()+=yoffset;
}


void Vector::Show(){
    cout<<this->GetStart().GetX()<<','<<this->GetStart().GetY()<<" : "<<this->GetEnd().GetX()<<','<<this->GetEnd().GetY()<<"\n";
}



int main(){
    Vector v(1,1,3,3);
    v.Show();
    v.OffSetVector(v.GetStart(),1,3);
    return 0;
}

Vlad de Moscou

La fonction GetStart renvoie un objet temporaire de type Point

Point GetStart();

tandis que la fonction OffsetVector excepte une référence non constante à un objet.

void OffSetVector(Point&,int,int);

Vous ne pouvez pas lier un objet temporaire avec une référence lvalue non cinstant.

Modifiez la déclaration de la fonction GetStart comme

Point & GetStart();

Aussi au moins la fonction GetEnd doit être modifiée comme

Point & GetEnd();

Vous pourriez surcharger les fonctions pour les objets constants et non constants

Point & GetSgtart();
cosnt Point & GetStart() const;
Point & GetEnd();
const Point & GetEnd() const;

Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.

En cas d'infraction, veuillez [email protected] Supprimer.

modifier le
0

laisse moi dire quelques mots

0commentaires
connexionAprès avoir participé à la revue

Articles connexes

TOP liste

  1. 1

    Filtrer le dataframe basé sur plusieurs colonnes d'un autre dataframe

  2. 2

    Laravel SQLSTATE [HY000] [1049] Base de données inconnue 'previous_db_name'

  3. 3

    Enregistrer le chemin de l'image de la galerie vers la base de données de la salle et l'afficher dans la liste des recycleurs

  4. 4

    Comment afficher du texte au milieu de div avec une couleur d'arrière-plan différente?

  5. 5

    Microsoft.WebApplication.targets

  6. 6

    Comment changer le navigateur par défaut en Microsoft Edge pour Jupyter Notebook sous Windows 10 ?

  7. 7

    Échec de l'exécution de 'insertBefore' sur 'Node': le paramètre 1 n'est pas de type 'Node'

  8. 8

    Empêcher l'allocation de mémoire dans la génération de combinaison récursive

  9. 9

    Comment analyser un fichier avec un tableau d'objets JSON en utilisant Node.js?

  10. 10

    comment afficher un bouton au-dessus d'un autre élément ?

  11. 11

    Comment centrer un div tout en utilisant la transition et transformer avec l'échelle

  12. 12

    Filtrer les données en fonction des conditions d'une trame de données

  13. 13

    ESP8266 HADRWARE MINUTERIE, USA pour cocher une macro étrange

  14. 14

    Comment définir du texte dans un QLabel et afficher les caractères '<>'?

  15. 15

    System.Data.SqlClient.SqlException: 'Nom de colonne non valide' ApplicationRoleId '.'

  16. 16

    Pourquoi Phantomjs ne fonctionne pas avec ce site ?

  17. 17

    Stop jQuery execution after one time execution

  18. 18

    Concaténer des variables dans ansible

  19. 19

    Comment calculer la probabilité du graphique de densité?

  20. 20

    php ajouter et fusionner des données de deux tables

  21. 21

    Redirection HTTP vers HTTPS dans Java à l'aide de HTTPURLConnection

chaudétiquette

Archive