Hi,

I am new to C++ and trying to write a function that returns a pointer to a structure.

I have a header file as follows.

class Blah
{
public:
struct Values *getValues();
struct Values
{
int A;
int B
double C;
}
}

My class file is as follows....

#include "Blah.h"

struct Values* Blah::getValues()
{
Values *ptr;
return ptr;
}

I get an error though that states Cannot convert 'Blah::Values*' to 'Values*'
Can anyone tell me what I am doing wrong? Thanks.

Because you declared the structure within the body of the class you have to use the class scop operator when referencing the structure. Its not necessary to use the struct keyword. Blah::Values *getValues();

Thank you for your reply Ancient Dragon. This may be a very stupid question but why do I have to use the scope resolution operator in the Class. Since I am using the function within the namespace of the class, why would I still have to define it with the :: operator.

It isn't strictly related to namespaces.
If you write a function outside of class it belongs, you have to use :: operator!
Otherwise, compiler won't know if it's some other function or that particular one.

It's not "a very stupid question". It's a good question.

You define Blah::getValue member function outside the scope of the Blah class (you declare it inside the class definition but define it outside the class definition). The struct Values is declared inside the Blah definition so its scope is class Blah, not enclosed scope where you place the function body. No such name as Values in this context. That's why you need to qualify Values with Blah scope in getValue header outside Blah class definition.

Nested type names declared inside other classes are not class member names. They are common type names with restricted scope.

Ahh, thank you very much Ark for your clear explanation. I understand now.

I have another question.

class TblkArray
{
      public:
      TblkArray();
      ~TblkArray();

      struct VoltageValues *getVoltageValues();
      void setVoltageValues(struct VoltageValues *ptrVoltageValues); ==HERE


      private:
      struct VoltageValues myVoltageValues;
};

How would I setup a SETTER function to access the values in that structure? I am a little bit unsure of how to do it since I am using a structure instead of a regular variable.

What's a problem? All member functions have all access rights to all members of this class.
You can assign structure variables by operator = or assign structure members (member by member). Can you explain your doubts?

This article has been dead for over six months. Start a new discussion instead.