Thread: calling function in class error

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    calling function in class error

    Hello

    The code I have:

    Code:
    someobject object();
    object.some_function();
    Will give error because of the second line..

    The error is: error C2228: left of '.some_function' must have class/struct/union

    I use MSVC 2005.

    How can I avoid that without changing the first line?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You can't (unless you invoke undefined behaviour, which is rarely a good idea). Your first line of code is a declaration of a function named object() that takes no arguments, and returns a someobject.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so
    Code:
    object().somefunction();
    will probably call this defined in the first line function and then - call the some_function member of the object returned by the function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But that's probably not what you want. If you just want to instantiate the object with the default constructor, just do
    Code:
    someobject object;
    object.some_function();
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM