Pointer member not being recognised properly

This is a discussion on Pointer member not being recognised properly within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there's a nack or hidden rules about this that I don't know about, because I'm struggling. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Pointer member not being recognised properly

    I was wondering if there's a nack or hidden rules about this that I don't know about, because I'm struggling.

    The situation is I have a class, 'User', which needs access to the database, which is accessed through 'class DBI'. Since the database connection is usually established before User is, the way I have it set up (for now) is to pass a reference to the initialised DBI class to the User constructor, which saves it as a member.

    I've tried using both a reference member and a pointer member, but I keep getting an error on a member functions that use it. The pseudo-code below is the general way I'm doing it.

    Code:
    class DBI {
    public:
        void query();
    };
    
    class User {
        DBI* mDBH;
    public:
        User( DBI& dbh ) : mDBH( &dbh ) {}
    
        void do_stuff() {
            mDBH->query();
        }
    };
    
    void main()
    {
        DBI db;
        User( db );
    }
    When compiling what's basically that, I get an error :
    left of '->query' must point to class/struct/union/generic type
    Is there something glaringly wrong with the above concept?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    User d( db ); would work better, I think. You have to create an instance. Right?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Oh, sorry. I do actually have a proper instance in the real code. Making that kind of stupid mistake makes me think that I've made one somewhere glaring, but I can't find it for the life of me.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Other than the fact that you havent defined DBI::query and you use void main (main will always return an int) I see nothing wrong with the above code. It also compiles and runs. What compiler are you using?

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    "left of pointer" is meaning somthing you have not declatred, like shakti suggested. If you are using void main I gather you are also using MSVC++6.0, it is the only compiler I know of that allows void main without a flag warning

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    I'm not actually using void main, just trying to cut out as much as possible. I'm using MSVC++ (the free one) btw.

    As far as not defining DBI::query(), it is defined in the class DBI is it not?

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    No, it is declared in class 'DBI', it is not defined. Meaning, DBI::query() doesn't actually do anything.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Why don't you use inheritance? It may be easier.

    Code:
    class DBI {
    public:
        void query(){std::cout<<"whatever";}
    };

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Thanks for the help. As far as defining the function goes, I actually have it defined in my program (and it works fine); but I should recognise the keywords more.

    If I used inheritance in this context - as in, inheriting a completely unrelated class (which I'd rather not have to do, mainly because it's not the style I see in any production code) - would there be any speed/size penalty for doing so? Would initialising through the copy constructor be just the same as using a pointer/reference member?

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Gah, I figured it out.

    The function that was getting the error was a static one. I am a true idiot, but quietly proud (hence true).

    Anyway, thanks for your help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. void pointer to a member function
    By DeepBlackMagic in forum C++ Programming
    Replies: 7
    Last Post: 07-18-2003, 06:01 PM

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