c++11 - return the answer from "void" to another function -


i got question how can return answer display because kept getting error void can't return variable. how can send display function.

 void rational::add(const rational&h2)     {          int num = 0;         int dem = 0;         add(h2);         int p = num * h2.dem + h2.num*dem;         int q = dem*h2.dem;       }      void display() const; // _p:_q     {          if (q == 1) // e.g. fraction 2/1 display 2                 cout << p << endl;             else                 cout <<p << "/" << q << endl;     } 

void return type of function defined, void means you're returning nothing. if had function "int rational::add(**)" can return int function.

from can see have few problems:

  • you call add() inside add() function, keep looping until have stack overflow

  • from can see have variable q , p , want display them, display isn't function belongs class can't access variables q , p assume defined in header.

  • if define q , p inside header function add() isn't writing them. when int q , int p in add() function you're defining new variable same name, writing local variable isn't going change q , p may intending write to. solve don't define q , p again , write without int.

    p = num * h2.dem + h2.num*dem;

    q = dem*h2.dem;


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -