'Accessing private fields and function from main

I've programmed only in Java in my career and started using C++ since 10 days, so this question may seem strange to many of you. I have defined the structure of a class in a header file:

#include "ros/ros.h"
#include "nav_msgs/Odometry.h"
#include "geometry_msgs/Pose.h"
#include "geometry_msgs/Point.h"
#include "stdio.h"
#include "sensor_msgs/LaserScan.h"
#include "list"
#include "vector"
#include "scan_node.h"
#include "odom_node.h"
#include "coord.h"

class stage_listener{
public:
    stage_listener();
    
private:
    std::list<odom_node> odom_list;
    std::list<scan_node> scan_list;
    std::list<coord> corners_list;
    
    std::list<coord> polar2cart(std::vector<float>, float, float, float, float);
    void addOdomNode (const nav_msgs::Odometry);
    void addScanNode (const sensor_msgs::LaserScan);
    void extractCorners(std::vector<float>, float, float, float, float);
    int distance (float, float, float, float, float);
    void nodes2text(std::vector<odom_node>, std::vector<scan_node>);
    int numOdom();
    int numScan();
};  

In the associated .cpp file, I wrote a main:

int main(int argc, char **argv){
        char buffer [1024];
        while(1){
            int i = fscanf(stdin,"%s",buffer);
            
            if(strcmp("exit",buffer) == 0)
                exit(0);
                
            else if(strcmp("num_nodes",buffer) == 0){
                ROS_INFO("Odometry nodes: %i\nScan nodes: %i",numOdom(),numScan());
            }
            
            else{}
        }

}

The ROS_INFO function is part of Willow Garage's ROS and you can intend it like a normal printf, taking exactly arguments in the same form. On compiling code, I get the following:

/home/ubisum/fuerte_workspace/beginner/src/stage_listener.cpp: In function ‘int main(int, char**)’: /home/ubisum/fuerte_workspace/beginner/src/stage_listener.cpp:223:5: error: ‘numOdom’ was not declared in this scope /home/ubisum/fuerte_workspace/beginner/src/stage_listener.cpp:223:5: error: ‘numScan’ was not declared in this scope Do you know the cause of the errors? In Java, you can access private fields/functions, so I can't understand the reason why in C++ it's not possible.


Solution 1:[1]

In Java, you can access private fields/functions

No you can't, unless you're using reflection. That's the entire point of making something private. I think you're mixing up public and private here. (You can access private static fields and methods in java from the classes own main method though). The main function in C++ is not associated with a class (and even if it were, your code still wouldn't work because you're attempting to access instance members statically).

Solution 2:[2]

There are a few things here.

Firstly, you need to make stage_listener object to call its methods or make methods static then use scope resolution operator to call the functions if you want objects to share same method. Secondly, you cannot access private variables( that is the point of data hiding in OO languages.)

So, you need to make functions public in the class and either create object of the class or make functions static and call them using scope resolution operator. Depends totally on what are you trying to accomplish. I hope this helps.

Solution 3:[3]

Usually, in C++, we use the functions as a public.

You did it as private, so just in the scope of own class you'll can access them. So, if yoy're trying to use them in the main, we can think that these functions should be public.

And you need to declare an object of the class. So you'll can access the function using object.function(); command.

Solution 4:[4]

1) You can't call methods without instantiating the class, or simply put you have to create object first.

2) Even if you create an object of your class your object can't call private methods. So make numOdom and numScan public methods.

Solution 5:[5]

You can't access any private method outside of on of this class's method (even in Java).

Set the methods you want to use outside as public

Solution 6:[6]

In C++, the main function is not part of the class you defined (even though it's "in the associated .cpp file"), so it can't access private fields. In C++ as in Java, only functions that are declared in the class are part of the class, and so can access private fields.

Solution 7:[7]

1) Public methods in the public section

2) To call the non-static public methods of the class, first create an instance of the class

    class stage_listener{
    public:
        stage_listener()
        {
        }

        //all methods are not implemented
        void addOdomNode (const nav_msgs::Odometry) {}
        void addScanNode (const sensor_msgs::LaserScan) {}

        int numOdom() { return 0; }
        int numScan() { return 0; }

    private:

        std::list<float> odom_list;
        ........

        std::list<float> polar2cart(std::vector<float>, float, float, float, float)
        {
                   //empty list
            return std::list<float>();
        }

        ........
    };  

    int main(int argc, char **argv){

            char buffer [1024];
            while(1){
                int i = fscanf(stdin,"%s",buffer);

                if(strcmp("exit",buffer) == 0)
                    exit(0);

                else if(strcmp("num_nodes",buffer) == 0){

                    //create object on the stack
                    stage_listener sl;

                    ROS_INFO("Odometry nodes: %i\n
                              Scan nodes: %i\n",
                              sl.numOdom(),
                              sl.numScan());
                }
                else{ 
                    return 1;
                }
            }
            return 0;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cubic
Solution 2 Mayank
Solution 3
Solution 4 StahlRat
Solution 5 Jeremie
Solution 6 antlersoft
Solution 7