function which returns a pointer to a class.

This is a discussion on function which returns a pointer to a class. within the C++ Programming forums, part of the General Programming Boards category; Hi, Here's the problem: I have a: Code: class g { class foo; ... foo* build(); ... }; foo g::*build() ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    function which returns a pointer to a class.

    Hi,
    Here's the problem:
    I have a:
    Code:
    class g
    {
            class foo;
            ...
            foo* build();
            ...
    };
    
    foo g::*build()
    {
            ...
    }
    And such code does not work - I mean that definition of foo *build() outside the class g. How should I code it?
    Regards.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    >> foo* build();
    The complete return type is "foo*". So...
    Code:
    foo* g::build() {}
    gg

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    foo g::*build()
    Minor change:
    Code:
    foo *g::build()
    [edit]Curses, foiled again.[/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    76
    But now I have that errors:
    y.cpp:19: error: expected constructor, destructor, or type conversion before '*' token
    y.cpp:19: error: expected `,' or `;' before '*' token
    Maybe I'll show the whole code:
    Code:
    #include <cstdio>
    
    class g
    {
            class foo;
            foo* build();
    };
    
    
    class g::foo
    {
            int y;
            foo(int i =0)
            {
                    y=i;
            }
    };
    
    foo* g::build()
    {
            foo *f;
            f=new foo;
            return f;
    }
    
    int main()
    {
            return 0;
    }
    What's wrong with it?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What's wrong with it?
    Two things: you don't qualify foo with g outside of the class or function body, and your constructor for foo is private, so it's no accessible from g unless you make it a friend. Try this instead:
    Code:
    #include <cstdio>
    
    class g
    {
      class foo;
      foo* build();
    };
    
    
    class g::foo
    {
      int y;
    public:
      foo(int i =0)
      {
        y=i;
      }
    };
    
    g::foo* g::build()
    {
      foo *f;
      f=new foo;
      return f;
    }
    
    int main()
    {
      return 0;
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Two things: you don't qualify foo with g outside of the class or function body
    i.e. you didn't qualify foo with g:: in some spots, and you need to do so.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i.e. you didn't qualify foo with g:: in some spots, and you need to do so.
    Was I being cryptic again?
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thank you very much guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function that returns a class...
    By cjmdjm in forum C++ Programming
    Replies: 16
    Last Post: 02-21-2009, 02:21 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 04:24 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21