2

I am working with OpenCV, more specifically the class shown below.

namespace cv_bridge {

class CvImage
{
public:
  std_msgs::Header header;
  std::string encoding;
  cv::Mat image;
};

typedef boost::shared_ptr<CvImage> CvImagePtr;
typedef boost::shared_ptr<CvImage const> CvImageConstPtr;

}

Once I create an object of CvImage how do I access the public values? They are also classes. For example I want to output the value of header.seq so I try to cout cv_bridge::CvImgPtr.header.seq but receive the error "expected primary expression before '.' "

How do I access this information?

varlotbarnacle
  • 821
  • 1
  • 6
  • 7
  • You need an instance of CvImage. Then you access it with object.header.seq – super Jun 29 '18 at 09:40
  • `cv_bridge::CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr& sub_msg, const std::string& encoding = std::string()); ` I use something like this. – varlotbarnacle Jun 29 '18 at 09:41
  • If I understand correctly... there will be a copy of the CvImage object "sub_msg" which has been altered by toCvCopy? – varlotbarnacle Jun 29 '18 at 09:44
  • 1
    It sounds like you're confusing classes with objects. There's a good list of books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jun 29 '18 at 09:48
  • ^ thank you for this I will look into it. However I do not have the time now. If create an object of CvImage, in public there are classes std_msgs::Header, std::string encoding, and cv::Mat image. What happens to them? – varlotbarnacle Jun 29 '18 at 09:50
  • `CvImagePtr ptr = std::make_shared(std::move(header), "UTF-8", std::move(image) ); ptr->header.seq` In any case you'd better use encapsulation. – Victor Gubin Jun 29 '18 at 11:04

1 Answers1

2

what you report is the class definition, in the definition as logic you need to specify the type(in this case the class) of the object you want inside the class. (EDIT + other typedef)

public:
  std_msgs::Header header;   //declare header of type std_msgs::Header 
  std::string encoding;
  cv::Mat image;

to decalre (made know to the program that he need to reserve a memory space for an object) an object of class cv_bridge::CvImage , you need to do:

cv_bridge::CvImage this_is_an_object;       //declaring an object of type CvImgage 

EDIT: in your case cv_bridge::CvImgPtr is a shared_ptr so a pointer ( typedef boost::shared_ptr<CvImage> CvImgPtr;) so it need to be used like a pointer.

(simplified: a pointer is a variable that contain the memory address of another variable or object, pointer is a variable that point to another, used to reduce memory copying when pass object to function for example)

so: to declare an object you type the name of the class (EDIT the name of a pointer of the class in this case CvImgPtr is a pointer for CvImage) (the name is useful to the compiler that with this declaration know how the memory reserved need to be structed, accoding to class CvImage definition) cv_bridge::CvImgPtr followed by the name of the object you want (that is called in this case this_is_an_object).

is like writing basic variable declaration (int j , double a, char f)

doing cv_bridge::CvImgPtr.header.seq has no meaning as you are mixing a variable declaration with invocation of name of an object of the class, and even a pointer in the half.

to call the object seq you need to type this_is_an_object.header.seq like if it is a struct.

struct foo{         //definition : teach the compiler how is made struct foo.
char bar;
};

foo aaa;           //aaa is the name of the variable/object/instance of the struct
aaa.bar = "a";     //call the member bar of the struct and assign value "a"
std::cout<<aaa.bar;   //print aaa.bar

similar in your case with classes:

class CvImage              //definition
{
public:
  std_msgs::Header header;
  std::string encoding;
  cv::Mat image;
};


cv_bridge::CvImage this_is_an_object;      //declare the variable / object this_is_an_object of class  cv_bridge::CvImage
std::cout<< this_is_an_object.header.seq;      //access the value of seq from object header of object this_is_an_object, 
//this_is_an_object is an object of class cv_bridge::CvImage

or:

typedef boost::shared_ptr<CvImage> CvImgPtr;
cv_bridge::CvImgPtr this_is_a_pointer_to_object;
std::cout<< (*this_is_a_pointer_to_object).header.seq; 

equivalent to:

std::cout<< this_is_a_pointer_to_object -> header.seq;

the operator (*object).member is equal to object->member

Alb_Sim
  • 49
  • 5