5
votes

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_list;
        std::list scan_list;
        std::list corners_list;

        std::list polar2cart(std::vector, float, float, float, float);
        void addOdomNode (const nav_msgs::Odometry);
        void addScanNode (const sensor_msgs::LaserScan);
        void extractCorners(std::vector, float, float, float, float);
        int distance (float, float, float, float, float);
        void nodes2text(std::vector, std::vector);
        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.

7
You have to include the header in the main file and make an object before you use the functions (which have to public in order to be used outside of the class). The latter is the same as in Java.chris
You've programmed only in Java and yet you think private members can be accessed outside the class? There's got to be something wrong with your question.Nikos C.
In Java, main is a class member (or whatever the Java terminology for that is) and so can access private members of that class - but not of others. Perhaps that's what you mean?Mike Seymour
I imported .h file, just omitted includes for brevity. what i meant is that, in java, you can access a private function/field if you do that from inside the main contained in the class where those functions/fields were definedubisum
@user1860467 What you're attempting still doesn't make sense. In Java, main is a static method. You'd still need to create a class instance for main to be able to invoke its methods, unless all methods in question are static as well, but clearly that's not the case here.Praetorian

7 Answers

6
votes

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).

2
votes

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.

2
votes

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.

2
votes

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.

1
votes

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

0
votes

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.

0
votes

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;
    }