0

I have a header file (abc.hpp) as bellow:

#include <iostream>
#include <stdio.h>
#include <vector>
using std::vector;
class ABC {
public:
        struct BoxPoint {
            float x;
            float y;
        } ;
        vector<BoxPoint> getBoxPoint();

Then, in the source file (abc.cpp):

#include "abc.hpp"
vector<BoxPoint> ABC::getBoxPoint(){
   vector<BoxPoint> boxPointsl;
   BoxPoint xy = {box.x1, box.y1};
   boxPointsl.push_back(xy);
   return boxPointsl
}

When I compile, there is error:

error: ‘BoxPoint’ was not declared in this scope

at line vector<BoxPoint> ABC::getBoxPoint()

If I change to void ABC::getBoxPoint(), (also change in the header file and remove return boxPointsl, the error does not exist. Can you point me to why display the error and how to resolve it? Thank you!

1
  • 2
    You're asking for vector<BoxPoint>, not vector<ABC::BoxPoint> Commented Oct 6, 2022 at 5:24

1 Answer 1

3

the symbol BoxPoint was only known inside ABC scope, outside you have to add the ABC scope, so ABC::BoxPoint:

#include "abc.hpp"
vector<ABC::BoxPoint> ABC::getBoxPoint(){
   vector<BoxPoint> boxPointsl;
   BoxPoint xy = {box.x1, box.y1};
   boxPointsl.push_back(xy);
   return boxPointsl
}

Inside the function you can use BoxPoint again, since we are inside the ABC scope again.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need the ABC:: prefix inside the function body, as it is considered in the scope.
Thank you very much! I solved my this problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.