calling function in class error

This is a discussion on calling function in class error within the C++ Programming forums, part of the General Programming Boards category; Hello The code I have: Code: someobject object(); object.some_function(); Will give error because of the second line.. The error is: ...

  1. #1
    l2u
    l2u is offline
    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,259
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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, 09: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, 04:27 AM

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