0

there is the code :

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class Flacon
{
private:
  string nom;
  double volume;
  double pH;

public:
    Flacon(string nom, double volume, double pH): nom(nom), volume(volume), pH(pH) {}
    ostream& etiquette(ostream&) const;
    friend ostream& operator<<(ostream&,const Flacon &);
    Flacon& operator+(Flacon const&);

};

Flacon& Flacon::operator+(Flacon const& f){
    this->nom=this->nom + f.nom;
    this->volume+= f.volume;
    this->pH=-log((this->volume * pow(10,-this->pH) + f.volume * pow(10,-f.pH))/(this->volume + f.volume));
    return *this;

ostream& Flacon::etiquette(ostream& sortie) const {
    sortie << nom << " : " << volume << " ml, pH " << pH;
    return sortie;}

I have the error :

error: qualified-id in declaration before ‘(’ token

At line 27 :

ostream& Flacon::etiquette(ostream& sortie) const {

Thank you for your help

I would also like to know why this.name displays an error when this->name does not display one

Hitan Elo
  • 63
  • 1
  • 10
  • 6
    You are missing `}` after `return *this;` and before `ostream& Flacon::etiquette...` (i.e. the closing brace for `operator+` function) – Yksisarvinen Apr 29 '20 at 10:04
  • Unrelated, but check out [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) `operator+` is not expected to modify any object and it will confuse anyone who uses `Flacon` class. – Yksisarvinen Apr 29 '20 at 10:06
  • Do you mean `this->nom` in the last question? – darclander Apr 29 '20 at 10:07
  • I put + in internal overload to avoid creating another useless variable in memory that will be the result of this addition, isn't that good? – Hitan Elo Apr 29 '20 at 10:08
  • Yes, i wanted to mean this->nom – Hitan Elo Apr 29 '20 at 10:09
  • 1
    @HitanElo You've writen an version of `operator+` that does not behave in the same way as a normal `operator+`, so no that's not good. In fact your `operator+` is just like `operator+=` so you should rename it. – john Apr 29 '20 at 10:10
  • 1
    @HitanElo Imagine I use this class. I have two objects: `Flacon a;` and `Flacon b;`. Now I write this line: `Flacon c = a + b;`. Quite reasonable, isn't it? But now both `a` and `c` contain summed value and I don't have the value from `a` anymore... – Yksisarvinen Apr 29 '20 at 10:11
  • 1
    `this->nom` works because `this` is a pointer, `this.nom` does not work because, well, `this` is a pointer. – john Apr 29 '20 at 10:12
  • I understand, you(re right, thank you ! – Hitan Elo Apr 29 '20 at 10:15

1 Answers1

0

The reason to why this.nom causes an error is because this is a pointer. I believe that you should be able to use (*this).nom instead if you are used to Java but that might be bad practise. I would personally suggest that you use this->nom.

See:

a.b is only used if b is a member of the object (or reference[1] to an object) a. So for a.b, a will always be an actual object (or a reference to an object) of a class.

And:

a →b is essentially a shorthand notation for (*a).b, ie, if a is a pointer to an object, then a→b is accessing the property b of the object that points to.

Source: https://www.tutorialspoint.com/What-is-arrow-operator-in-Cplusplus

darclander
  • 1,526
  • 1
  • 13
  • 35