Error with typedef inside a class...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    Error with typedef inside a class...

    I wrote the following code
    [code=cpp]
    #include <map>
    #include<iostre am>
    using namespace std;

    //typedef map<int,int> Maps;

    class A
    {
    public:
    typedef map<int,int> Maps;

    private:
    Maps *m_MapP;
    public:
    Maps* getMapP();
    int* getIntP();
    };

    Maps* A::getMapP()
    {
    return m_MapP;
    }

    int* A::getIntP()
    {
    return NULL;
    }


    int main()
    {
    return 0;
    }
    [/code]

    When i compile this i am getting compiler error in the method definition of getMapP.
    But when i move the typedf declaration above the class then it is working fine.
    I cant figure out why this is happeneing.
    can somebody comment on this.

    Thanks
    Raghuram
  • Joby Allen
    New Member
    • Apr 2008
    • 5

    #2
    Originally posted by gpraghuram
    I wrote the following code
    [code=cpp]
    #include <map>
    #include<iostre am>
    using namespace std;

    //typedef map<int,int> Maps;

    class A
    {
    public:
    typedef map<int,int> Maps;

    private:
    Maps *m_MapP;
    public:
    Maps* getMapP();
    int* getIntP();
    };

    Maps* A::getMapP()
    {
    return m_MapP;
    }

    int* A::getIntP()
    {
    return NULL;
    }


    int main()
    {
    return 0;
    }
    [/code]

    When i compile this i am getting compiler error in the method definition of getMapP.
    But when i move the typedf declaration above the class then it is working fine.
    I cant figure out why this is happeneing.
    can somebody comment on this.
    Because the Maps typedef is inside your class, you need to scope it accordingly.

    [code=cpp]
    A::Maps* A::getMapP()
    [/code]

    should do the trick.

    Regards,

    -Joby

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Originally posted by Joby Allen
      Because the Maps typedef is inside your class, you need to scope it accordingly.

      [code=cpp]
      A::Maps* A::getMapP()
      [/code]

      should do the trick.

      Regards,

      -Joby
      What u say is right.
      But why we have to specify the scope resolution for the return type alone.
      But for argument you need not specify this.
      Check this code
      [code=cpp]
      A::Maps* A::getMapP()
      {
      }
      A::Maps* A::getMapP(Maps *p)//This works fine without A::
      [/code]

      My doubt is what is C++ syntax for this typedef,,,that is what my question is.


      Raghuram

      Comment

      • Joby Allen
        New Member
        • Apr 2008
        • 5

        #4
        Originally posted by gpraghuram
        What u say is right.
        But why we have to specify the scope resolution for the return type alone.
        But for argument you need not specify this.
        Check this code
        [code=cpp]
        A::Maps* A::getMapP()
        {
        }
        A::Maps* A::getMapP(Maps *p)//This works fine without A::
        [/code]

        My doubt is what is C++ syntax for this typedef,,,that is what my question is.


        Raghuram
        Interesting. My guess here is that the default scope of the arguments of a method are the same as the scope of the method itself. In other words, the compiler checks A:: for a definition of Maps before checking globally.

        As I said, this is just a guess, maybe a browse through Stroustrup will explain this definitively.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          What i thought is
          The typedef gets resolved only inside the class method and the class method signature.
          As return type is not part of signature , i think this is not getting resolved.

          Other experts please correct me if i am wrong

          Raghuram

          Comment

          Working...