-2

My code has a class compROS. I have created 2 functions requestStart and requestNotStart which are trying to call is_started_ and sec_ from class compROS. Now whenever I run the code, I get the following errors:

functions requestStart and requestNotStart and is_started_ and sec_ were not declared in this scope.

My assumption is that the private functions are not accessible from the outside of the class. Should I add requestStart and requestNotStart as friend functions??

What is the most efficient way of tackling these errors?

Following is my code -

(Updated my code based on the comments from @Snps and @Philip Brack)

using namespace std;
namespace Lib
{
  class compROS
  {
    public:
      compROS(string error_text, int sec):
        error_text_(error_text),
        is_started_(false),
        sec_(sec)
        {
        }

    private:
      string error_text_;
      bool is_started_;
      int sec_;
  };
}

int requestStart(Lib::compROS& c)
{
  if(!c.is_started_)
    sec_ = 2;
    // Start timer
    // Timer expired
  c.is_started_ = true;
  return 0;
}

int requestNotStart(Lib::compROS& c)
{
  // <Code to be inserted>
  return 0;
}

int main (int argc, char **argv)
{
  Lib::compROS c("error", 2);
  requestStart(c);
  requestNotStart(c);

  return 0;
}
roo_saa
  • 75
  • 6
  • 1
    `requestStart()` is not a member of your class, which means `sec_` and `is_started_` are not defined inside `requestStart()` and you can't use them. – DimChtz Nov 01 '17 at 16:47
  • It seems you need a general revision about c++..that code makes no sense. – Federico Nov 01 '17 at 16:50
  • 1. `is_started_` and `sec_` are not functions. You are not trying to treat them like functions, either. It’s confusing if you call them functions. 2. When you get a compiler error, please *copy* the text of the compiler error, not re-type the parts you think are important. – Daniel H Nov 01 '17 at 16:54
  • 1
    See https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1 – Chiel Nov 01 '17 at 17:52
  • Need an example of your intended usage. Class member functions and friend free functions may be equally viable options. – user4581301 Nov 01 '17 at 17:57
  • I apologize for this noob question. I have just started coding in C++ (still learning the concepts) and definitely new to Stackoverflow. Thank you for your help. – roo_saa Nov 01 '17 at 21:20

3 Answers3

1

int compROS::requestStarted() { } after the class definition will scope the member function to your object. Your issue is that you are declaring a function but not binding it to compROS class so you cannot access the instance members.

Philip Brack
  • 1,340
  • 14
  • 26
  • Thank you @Philip . I have a follow-up question to this. After binding my function to the class in the way you suggested, will I be able to access the variables declared in `private`? – roo_saa Nov 01 '17 at 21:22
  • yes. In C++ member functions have access to private members. – Philip Brack Nov 01 '17 at 21:31
  • I have updated my code in the original post. Can you kindly have a look? I am still not able to access the private members. – roo_saa Nov 01 '17 at 22:18
0

When you create a class in c++, what is usually included are the functions that this class will use(or may use), as well as the variables it needs in order to runs those functions. In your case, your requestStart() and requestNotStart() functions are not included in your compROS class as you don't have the actual function calls in your class. This is why when you try to run your program, your functions requestStart() and requestNotStart() are trying to find the variables is_started_ and sec_ but does not find it since those variables only exist INSIDE the class whereas your functions are OUTSIDE of your class. My suggestion is to put the function signature for both the requestStart() and requestNotStart() method in public: and simplify your compPOS method by only using the function signature and actually implement the method outside of the class.

  • Hi @Patrick Chu. Can you kindly elaborate the last line of your comment? What do you mean by simplifying the `compROS` method? – roo_saa Nov 01 '17 at 21:29
  • I am explaining under the assumption that your comROS is a constructor method. Essentially what you can do is implement the actual method outside of the class brackets, {}, and only use the signature, `comROS(string error_text, int sec)` in the brackets for your comROS class – Patrick Chu Nov 03 '17 at 08:39
0

In object-oriented programming, a class definition is like a blueprint of a house. You can not use the kitchen, nor the bathroom, of the blueprint, the only thing you can do is to use it to build a house.

In the same way, you can not call any methods of your class until you have built an instance of that class.

Lib::compROS c("my error", 2);

Any function that wants to call any of your class' methods needs to know on what instance to make the call. You need to somehow pass a reference to an instance of your function.

int requestStart(Lib::compROS& c) {
    if(!c.is_started_)
        sec_ = 2;
        // Start timer
        // Timer expired
    c.is_started_ = true; // This member needs to be public for external access.
    return 0;
}

int main() {
    Lib::compROS c("my error", 2); // Create instance.
    requestStart(c); // Pass instance to function.
}
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
  • I followed your suggestions and updated my code. But I still can't access the private members. – roo_saa Nov 01 '17 at 22:20
  • @dee19 Private members can not be accessed from outside the class. You need to either make the members public, create an accessor method (e.g. a setter), or define a friend function. – Felix Glas Nov 01 '17 at 22:35