is it possible to pass a structure element by reference?

This is a discussion on is it possible to pass a structure element by reference? within the C++ Programming forums, part of the General Programming Boards category; hi,is it possible to pass a structure element by reference or do you have to pass the whole structure?Googled it ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    is it possible to pass a structure element by reference?

    hi,is it possible to pass a structure element by reference or do you have to pass the whole structure?Googled it and just got passing the structure it self by reference.

    tried returning an element via a function but keep getting errors for the function definition.

    Code:
    int hullDam(sShip & playerShip.hull)
    {
    	return   playerShip.hull=playerShip.hull-10;
    }
    and here's the error report
    Code:
    my documents\visual studio 2008\projects\test\test\test.cpp(12) : error C2143: syntax error : missing ',' before '.'
    c:\documents and settings\xxxxxxx\my documents\visual studio 2008\projects\test\test\test.cpp(14) : error C2440: 'return' : cannot convert from 'int' to 'sShip'
            No constructor could take the source type, or constructor overload resolution was ambiguous

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    sorry!!

    Sorry about that was suppose to start a thread

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    is it possible to pass a structure element by reference?

    hi,is it possible to pass a structure element by reference or do you have to pass the whole structure?Googled it and just got passing the structure it self by reference.

    tried returning an element via a function but keep getting errors for the function definition.

    Code:
    int hullDam(sShip & playerShip.hull)
    {
    	return   playerShip.hull=playerShip.hull-10;
    }
    and here's the error report
    Code:
    my documents\visual studio 2008\projects\test\test\test.cpp(12) : error C2143: syntax error : missing ',' before '.'
    c:\documents and settings\xxxxxxx\my documents\visual studio 2008\projects\test\test\test.cpp(14) : error C2440: 'return' : cannot convert from 'int' to 'sShip'
            No constructor could take the source type, or constructor overload resolution was ambiguous

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    int hullDam(sShip & playerShip.hull)
    Parameters are just like any other variables . . . they're of the form
    Code:
    type variable_name
    So I think you probably want
    Code:
    int hullDam(sShip & playerShip)
    if you want to pass a whole sShip object, or maybe
    Code:
    int hullDam(int hull)
    if you just want to pass playerShip.hull. Those two would be called as
    Code:
    hullDam(playerShip);
    hullDam(playerShip.hull);
    respectively.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    You certainly can pass the member of a structure by reference, since it is just like any other object. However, the syntax would also be like how you declare any other reference parameter:
    Code:
    int hullDam(sShip& hull)
    {
        return hull -= 10;
    }
    Then you would call it as:
    Code:
    hullDam(playerShip.hull);
    Though I am not sure why you want to return an int, and if subtracting 10 from the sShip object is actually legal and makes sense since the compiler complains that it "cannot convert from 'int' to 'sShip'".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    thanks@dwks that got it to compile fine

    @laserlight im not sure i follow you..should the return type for the function be sShip?should structure elements not change?

    thanks

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think Laserlight was trying to solve a different problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Knowing when function needs pass by reference.
    By bsdunx in forum C Programming
    Replies: 4
    Last Post: 08-29-2007, 12:38 PM
  2. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM

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