0

I am using a code that stores points into a Vector data type as follows:

    RECT head;
    head.left = pt1.x;
    head.right = pt2.x;
    head.top = pt1.y;
    head.bottom = pt2.y;

    detectBox->push_back(head);

This falls inside a function that has a for loop that stores multple instances of "head" into the detectBox. This function is delared like this:

void GetHeads(cv::Mat Img, vector<RECT>* detectBox)

Where Img is just a normal black and white image being fed in with the heads that have been extracted through other processes. My question now is how to I see the points that have been stored inside of detectBox? I would like to access them outside of the for loop to use for other things. When i try and print out the variables Ive only been able to have the addresses returned (0000004FA8F6F31821, etc)

Also, detectBox is a shade of grey in the code(not sure what that means).

Full code can be found in my other question related to the same function, here:

C++ CvSeq Accessing arrays that are stored

-----EDIT-----

Methods tried and associated errors/Outputs:

First:

std::cout << "Back :  " << &detectBox->back << std::endl;

'&': illegal operation on bound member function expression

Second:

std::cout << "Back :  " << detectBox->back << std::endl;

'std::vector>::back': non-standard syntax; use '&' to create a pointer to member

Third:(Note, No Error, but no useful information output)

    std::cout << "Detect Box :  " << detectBox << std::endl;

Detect Box : 00000062BF0FF488

Fourth:

std::cout << "Detect Box :  " << &detectBox[1] << std::endl;

Detect Box : 000000CB75CFF108

Community
  • 1
  • 1
James Mallett
  • 827
  • 4
  • 11
  • 27
  • "print variables" - how are you printing them, let's see a bit more of your code. Without seeing it though, I'd guess that you're printing the pointer of the variable you passed in rather than the contents of the vector. – UKMonkey Sep 30 '16 at 16:27
  • Shade of grey - could be anything or nothing, but usually means 'unused'. – UKMonkey Sep 30 '16 at 16:27
  • @UKMonkey By printing the variables, I mean that I simply want to use "cout" to find out what is stored inside – James Mallett Sep 30 '16 at 16:42
  • But we don't know how you're trying to print them - and given that it's the printing that's going wrong it's a fairly major part of the question you're skipping on. – UKMonkey Sep 30 '16 at 16:45
  • See the added link for the code :) – James Mallett Sep 30 '16 at 16:46

2 Answers2

1

First, detectBox is a pointer to the array so it needs to be dereferenced before trying to index into it.

std::cout << (*detectBox)[1];  // Outputs the 2nd RECT in the vector
                               // assuming an operator<< is implemented
                               // for RECT.

Including the address of '&' operator before it will print out the address of the 2nd RECT instance at index 1 which you do not need to do because the index operator gives you the reference.

std::cout << &(*detectBox)[1];

Without an operator<< for the RECT instance to output it to the stream you will need to access the members directly. The following should work:

std::cout << "Left: " << (*detectBox)[1].left;
std::cout << "Right: " << (*detectBox)[1].right;
std::cout << "Top: " << (*detectBox)[1].top;
std::cout << "Bottom: " << (*detectBox)[1].bottom;

This can be improved by saving off a reference to the RECT you are trying to get first and then using that:

RECT& secondRect = (*detectBox)[1];
std::cout << "Left: " << secondRect.left;
std::cout << "Right: " << secondRect.right;
std::cout << "Top: " << secondRect.top;
std::cout << "Bottom: " << secondRect.bottom;

Finally, I notice that you push the new RECT on the back of the Vector, but then you always output the 2nd RECT in the vector. Assuming you want to print the RECT you just added you could either output the head local variable or use the back() method on the vector as so:

RECT& lastRect = detectBox->back();
std::cout << "Left: " << lastRect.left;
std::cout << "Right: " << lastRect.right;
std::cout << "Top: " << lastRect.top;
std::cout << "Bottom: " << lastRect.bottom;
brader24
  • 485
  • 2
  • 10
  • Thanks, that really helped. On another note, how would I then find out how many different elements are stored inside of detectBox? My Idea is to then loop through and display all of the vectors inside, as you showed. I tried using _sizeof(*detectBox)_ , but only get an answer of 32 – James Mallett Oct 01 '16 at 19:13
0
detectBox->push_back(head);
std::cout << &detectBox[1];

Unless you've got an overloaded operator "<<" for RECT (which you probably don't) it's going to try and match this to the best thing it can, which clearly isn't going to be what you want it to be.

Try something more like

std::cout << "L: " << &detectBox[1].left << 
             "R: " << &detectBox[1].right << std::endl

I'd also be worried that you're using [1] when what you probably want is ->back()

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • I added some information about the different errors im getting. Also when i used .left and .right as you suggested it gave an error stating that........... "'left' is not a member of 'std::vector, std::allocator<_Ty>>'" – James Mallett Sep 30 '16 at 17:30