Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c++ - typedef'd type not visible as return type of a member function

This program fails to compile(using gcc-4.5). The error message says:

error: ‘myType_t’ does not name a type

  1 class abc{
  2 //typedef int myType_t;
  3 
  4   public:
  5 typedef int myType_t;
  6 
  7     abc();
  8     myType_t fun1();
  9 };
 10 
 11 myType_t abc::fun1()
 12 {
 13   return 0;
 14 }
 15 
 16 int main()
 17 {
 18   abc abc1;
 19   return 0;
 20 }

Now declaring typedef int myType_t; outside the class abc makes this compile. My confusion is, what is the problem if the return type of a member function is typedef'd inside the class.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the C++ Standard:

9.9 Nested type names [class.nested.type]

Type names obey exactly the same scope rules as other names.In particular, type names defined within a class definition cannot be used outside their class without qualification.

class X {
public :
   typedef int I;
   class Y { /  . . .  / };
   I a;
};

I b;            // error
Y c;            // error
X::Y d;         // OK
X::I e;         // OK

So You need to access it as:

abc::myType_t abc::fun1()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...