Search code examples
c++eclipsestructreturneclipse-cdt

Struct Type Could Not Be Resolved C++ in Eclipse


We are doing a project in C++ using Eclipse in Ubuntu 12.04LTS and ROS environment. I used to define classes in source files, and skip the prototypes; but now we have been informed to use header files as prototypes, and source files as the extension of the functions whose prototypes are contained in the header files. It is aimed to achieve an easy-to-read code.

However, I have problems with returning the struct types, while it was no problem with the way I work before. Let me make myself clear about what I'm trying to achieve,

This is header file,

class Im_Robot
{
    public:
        Im_Robot();

    private:
        typedef struct
        {
            double d_kp1;
            double d_ki1;
            double d_kd1;
            double d_kp2;
            double d_ki2;
            double d_kd2;
        } ANGULAR_PARAMS;
            ANGULAR_PARAMS angular_params;

    protected:
        void SetAngularParams(double d_kp1, double d_ki1,double d_kd1, double d_kp2,double d_ki2,double d_kd2);
        ANGULAR_PARAMS GetAngularParams();
        ......

and this is source file,

void Im_Robot::SetAngularParams(double d_kp1,
                                    double d_ki1,
                                    double d_kd1,
                                    double d_kp2,
                                    double d_ki2,
                                    double d_kd2)
{
    // this, örneği çıkarılan objeyi temsil eden anahtar kelimedir.
    this->angular_params.d_kp1 = d_kp1;
    this->angular_params.d_ki1 = d_ki1;
    this->angular_params.d_kd1 = d_kd1;
    this->angular_params.d_kp2 = d_kp2;
    this->angular_params.d_ki2 = d_ki2;
    this->angular_params.d_kd2 = d_kd2;
}

ANGULAR_PARAMS Im_Robot::GetAngularParams()
{
    return this->angular_params;
}
   ......

In the source file and the correspondent line

ANGULAR_PARAMS Im_Robot::GetAngularParams()

"Type ANGULAR_PARAMS could not be resolved." error shows up. Even though I've searched it on the internet, I wasn't able to see any reliable solutions. Any answer will be much appreciated. Thanks already..


Solution

  • ANGULAR_PARAMS is a nested class who's scope is its enclosing class. The fully qualified name must be used:

    Im_Robot::ANGULAR_PARAMS Im_Robot::GetAngularParams()
    {
        return this->angular_params;
    }
    

    Other:

    • The typedef is unrequired in C++, just use struct ANGULAR_PARAMS.
    • There is an access specifier inconsistency as ANGULAR_PARAMS is a private class of Im_Robot but GetAngularParams() is protected meaning classes dervied from Im_Robot can call the function but cannot use the ANGULAR_PARAMS name explicitly (see demo ). It is possible to use auto to get access to the returned instance though (see demo and Why can I use auto on a private type?).