-7

So i've been trying to use the returned values of certain methods from my class in another methods , but I bump into this problem "Unable to resolve identifier"

class Puncte{
public:
    double mx;
    double my;

    Puncte(double x, double y) {
        mx = x;
       my = y;
    }
    double distanta (){
        double r = sqrt(mx*mx + my*my);
        return r;
    }
    double phi (){
        double unghi = atan(my/mx) * 180.0 / PI;
        if(unghi > 0 && mx < 0 && my < 0) unghi = unghi + 180;
        if (unghi < 0 && mx>0 && my < 0) unghi = unghi + 360;
        if (unghi < 0 && mx<0 && my > 0) unghi = unghi + 180;
        return unghi;
    }
    virtual  mprint(){

        printf("r=%.3f; phi=%.3f\n",r,unghi) ; //unable to resolve identifier
    }
};

Can someone please help? Thanks :)

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 4
    `printf("r=%.3f; phi=%.3f\n",distanta(),phi());` You need to call the functions, not try and access there return value. – super Oct 24 '18 at 21:10
  • Also see ["the Scope of (Local) Variables'](https://www.codesdope.com/cpp-scope-of-variables/) - the first example also shows how to use a return value from an invoked function. – user2864740 Oct 24 '18 at 21:15

1 Answers1

1

Using @super's suggestion and a little warning fixing.

The two important changes are in the line as suggested by @super:

printf("r=%.3f; phi=%.3f\n",distanta(),phi());

The variables 'r' and 'unghi' are both variables local to member functions and cannot be accessed outside of those functions. The "distanta()" and "phi()" member functions provide exactly what is needed in the printf and are the perfect way of getting the values needed.

The other change is explicitly stating that mprint() is a void:

void mprint(){

All functions in C++ (even main!) must have a return type. Also removed 'virtual' as there is no derived class (and I would have had to move the function to the public section of the class to keep it as virtual).

I have added the includes and the main() function so that anyone reading this code may cut and paste it into their compiler to get a working version without any more effort.

Further cleanup turns mx and my into private variables to prevent the user of the class from being able to modify these variables.

#include <cmath>
#include <cstdio>
#include <iostream>


class Puncte{
private:
    double mx;
    double my;
public:
    Puncte(double x, double y) {
        mx = x;
        my = y;
    }
    double mx() {return mx;}
    double my() {return my;}

    double distanta (){
        double r = sqrt(mx*mx + my*my);
        return r;
    }
    double phi (){
        double unghi = atan(my/mx) * 180.0 / M_PI;
        if(unghi > 0 && mx < 0 && my < 0) unghi = unghi + 180;
        if (unghi < 0 && mx>0 && my < 0) unghi = unghi + 360;
        if (unghi < 0 && mx<0 && my > 0) unghi = unghi + 180;
        return unghi;
    }
void mprint(){

        printf("r=%.3f; phi=%.3f\n",distanta(),phi()) ; //unable to resolve identifier
    }
};

int main() {
    Puncte puncte(3.5, 8.9);

    std::cout << "Point(" << puncte.mx() << ", " << puncte.my() 
        << "): radius = " << puncte.distanta() << std::endl;

    return 0;
}

Output:

$> main
Point(3.5, 8.9): radius = 9.56347

Process finished with exit code 0
Gardener
  • 2,591
  • 1
  • 13
  • 22
  • @user4581301, Helpful suggestions. – Gardener Oct 24 '18 at 22:07
  • 1
    Not overly related, simply FYI, but you should prefer [to include rather than ](https://stackoverflow.com/questions/13889467/should-i-include-xxxx-h-or-cxxxx-in-c-programs) – Tas Oct 24 '18 at 22:41
  • I made a public section for the access functions and removed the virtual keyword because it seems superfluous without a derived class or a suggestion that one is needed. – Gardener Oct 24 '18 at 22:44
  • Helpful suggestions, @user45813011. I did not know about the cxxxx include files. I will remember that one! – Gardener Oct 24 '18 at 22:51