Question:using struct inside the class as return type

Example:
// in header file
class Test {
private:
struct Test1 {
string s1;
string s2;
};
public:
Test1 testing(Test, string, string);
};

// in cpp file
struct Test1 Test::testing(Test1 str, string ss, string sss) {
str.s1 = ss;
str.s2 = sss;
return str;
}

There is something wrong with "struct Test1"
If i change "struct Test1" to "void" or move struct Test1
outside of class Test.It will be fine

I want to keep the struct inside the class.
Is there anyway to solve this problem?

Thank You
Last edited on
Consider the compiler parsing this definition. When it encounters Test1, it does not yet know that it is processing the definition of a method of Test. If it did, it would check Test for the Test1 class. Since it doesn't, it does not know how to resolve the symbol.

Similarlly, yet differently, the parameter Test1 is read after the compiler knows that it is processing a method of Test. Since it does, it checks Test for a Test1. There is much more to the process than that but this explanation should shed some light on what's going on.

Try Test::Test1 Test::testing(Test1 str, string ss, string sss) {.
Last edited on
Topic archived. No new replies allowed.