Home » Cannot use method returned value into another method

Cannot use method returned value into another method


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=%.3fn",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{
    double mx;
    double my;
    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=%.3fn",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;


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

Process finished with exit code 0

Related Solutions

Java simple accounting program [closed]

There's a spelling mistake (an extra 'e' on the input parameter annualInterest*e*Rate): public void setAnnualInterestRate(double annualInteresteRate){ this.annualInterestRate = annualInterestRate; } Therefore you are setting the this.annualInterestRate to...

pc and mobile site code using user agent

You could do something like this through media queries <link rel="stylesheet" media="screen and (min-device-width: 1024px)" href="https://stackoverflow.com/questions/21937880/pc.css" /> <link rel="stylesheet" media="screen and (max-device-width:...

How many CPU ticks does it take to store a single None? [closed]

In short: In this case, since X does not implement Drop, storing None is done in a single instruction. The number of CPU cycles depends heavily on the exact hardware and on caching effects. But assuming a modern x86-64 CPU and assuming the memory is in L1 cache...

sql aggregate and split [closed]

This uses STRING_AGG to aggregate the strings into one long one, and then a Tally Table to split into the new rows. It is assumed you have a column to order by. if you do not, you cannot achieve what you are after without one as data in a table is stored in an...

What is “-bash: !”: event not found”

You can turn off history substitution using set +H. ! is a special character to bash, it is used to refer to previous commands; eg, !rm will recall and execute the last command that began with the string "rm", and !rm:p will recall but not execute the last...

Visualize a sparse matrix using Python Turtle graphics

What does the problem mean by (rows-1, columns-1)? This is tied up with your mysterious m variable and the order() function you left undefined. Let's proceed anyway. We can see from the matrix() function we're dealing with a square matrix but let's not even...

What does an “extra” semicolon means? [duplicate]

A for loop is often written int i; for (i = 0; i < 6; i++) { ... } Before the first semicolon, there is code that is run once, before the loop starts. By omitting this, you just have nothing happen before the loop starts. Between the semicolons, there is the...

Python: Compare lists, perform operation, create new list

I think the downvotes are justified because you did not provide any code at all to show us what you tried so far, so that we know where you are stuck with your code. Also i think the way you wrote the requirements is a bit confusing (0eth probably means nth...

Python put multiple line array into a single line

I think you did not the best job at describing the problem, it is not really clear why you have such files (i guess most people were assuming you have an existing array in python) containing an array definition as if it was written for a specific language with...

Sort array contains numeric string using c# array

You need give an order (actually a equivalence relation) to be able to sort. The order on characters is usually a,b,c,... and the order usually given on words such as 'one' is called lexicographic order. However you want to sort by the meaning behind, its...

Sort (hex) colors by Hue [closed]

In this updated example also we don't care about hash, we'll just deal with an arrays of hex colors: require 'paint' #intantiate web_colors from gist and shuffle them eval(`curl...

read file string and store in uint8_t array in c [closed]

Is that what you wanted? (to test give binary 01 combination as an first argument) #include <stdio.h> #include <stdint.h> uint8_t charToBin(char c) { switch(c) { case '0': return 0; case '1': return 1; } return 0; } uint8_t CstringToU8(const char *...

my JavaScript using jQuery is not working [closed]

You didn't include jQuery in your code: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript" src="https://stackoverflow.com/questions/43397734/temp.js"></script> Make...

How to check if a Date falls in a certain week or Quarter [closed]

I would say do these steps, 1 - figure out first and last day of the week Get current week start and end date in Java - (MONDAY TO SUNDAY) 2 - check if the date falls in between that range or not Java: how do I check if a Date is within a certain range? - if...

How to add two object together

You should use a numeric data type for arithmetic operations (not Object). With out seeing the code in add() my recommendation is to store the total price in a double primitive. double price =0; for(int x = 0; x < rprice.size(); x++) { //you may need to...