Fixing a 'Invalid use of Member' Error in C++

  • Context: C/C++ 
  • Thread starter Thread starter lewis198
  • Start date Start date
  • Tags Tags
    C++ Error Member
Click For Summary

Discussion Overview

The discussion revolves around resolving an 'Invalid use of Member' error encountered in a C++ program. Participants explore issues related to class member access, function invocation, and proper syntax in the context of object-oriented programming.

Discussion Character

  • Technical explanation, Debate/contested, Homework-related

Main Points Raised

  • One participant describes their approach to defining a class with public members and member functions that compute values based on those members, but encounters an error when trying to print the result of a member function.
  • A later reply corrects the member access syntax, indicating that the member variables should be accessed through the object instance (e.g., x.member1) rather than directly.
  • Another participant points out the need to invoke the member function with parentheses (e.g., x.member_function()) to properly call it.
  • There is a mention of the necessity for a semicolon after the class declaration and the importance of declaring members as public to avoid default private access.
  • Areas of Agreement / Disagreement

    Participants generally agree on the corrections needed regarding member access and function invocation, but the initial poster's understanding of class structure and member function usage remains unresolved.

    Contextual Notes

    Limitations include potential misunderstandings about class member access and function invocation in C++, as well as the need for clarity on member visibility (public vs. private).

    Who May Find This Useful

    Readers interested in C++ programming, particularly those learning about object-oriented concepts and common syntax errors in class definitions and member function usage.

lewis198
Messages
95
Reaction score
0
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.
 
Technology news on Phys.org
#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.
 
Hi Ho!

Code:
member1=value;
member2=value;
member3=value;

It should be:
Code:
x.member1=value;
x.member2=value;
x.member3=value;
because they belong to the object, not to the class.

Also,
Code:
printf("%d",x.member_function);
should be
Code:
printf("%d",x.member_function());
because you invoke a method.Eus
 
Last edited:
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.
 
thanks guys, appreciate it
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K