Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Class conundrum

  1. Mar 9, 2008 #1
    Hi guys, I was wondering if you could help me with the following:

    1.I declare a class, then put public members into it.
    2.Then I make member functions that return values that are computed from the values of other members. These functions would have no input, I guess, so I gave them none. I can actually compile up to this point.
    3.Then I went onto the main part of the program, int main, and defined a class, and gave the members that are part of the other member functions values. I thought this would then give me a function that I could print values off, but when I tried to, the error message was:

    'invalid use of member. Did you forget the '&'?'



    The file looks like this:

    class name
    {

    member 1
    member 2
    member n

    member_function()
    {}

    }

    int main()
    {

    class name x;
    member1=value;
    member2=value;
    member3=value;

    printf("%d",x.member_function);
    }



    Thanks for your time.
     
  2. jcsd
  3. Mar 9, 2008 #2
    #3 should read:

    3.Then I went to the main part of the program, int main(), instantiated a class, and gave the properties of that class, their proper values.
     
  4. Mar 9, 2008 #3

    Eus

    User Avatar

    Hi Ho!

    Code (Text):

    member1=value;
    member2=value;
    member3=value;
     
    It should be:
    Code (Text):

    x.member1=value;
    x.member2=value;
    x.member3=value;
     
    because they belong to the object, not to the class.

    Also,
    Code (Text):

    printf("%d",x.member_function);
     
    should be
    Code (Text):

    printf("%d",x.member_function());
     
    because you invoke a method.

    Best regards,
    Eus
     
    Last edited: Mar 9, 2008
  5. Mar 9, 2008 #4

    KTC

    User Avatar

    Assuming this is C++, there's a ';" after the closing brace of the class declaration, and you need to declare the various members to be public as otherwise they are private by default.
     
  6. Mar 9, 2008 #5
    thanks guys, appreciate it
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Class conundrum
  1. Creating a class (Replies: 2)

  2. Wrapper Classes (Replies: 3)

  3. Classes and objects (Replies: 6)

  4. Pointers and classes (Replies: 1)

  5. Compression conundrum (Replies: 7)

Loading...