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

In summary, the conversation discusses declaring a class and its public members, creating member functions that return values based on other members, and encountering an error when trying to print values from the function. The correct syntax for accessing members and invoking methods is also mentioned.
  • #1
lewis198
96
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
  • #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.
 
  • #3
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:
  • #4
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.
 
  • #5
thanks guys, appreciate it
 

1. What does 'Invalid use of Member' error mean in C++?

'Invalid use of Member' error in C++ means that there is an issue with how you are accessing or using a member of a class. This could be due to a syntax error or attempting to access a private member.

2. How do I fix an 'Invalid use of Member' error in C++?

To fix an 'Invalid use of Member' error in C++, check your syntax and make sure you are using the correct member access operator (e.g. '.' for objects, '->' for pointers). Also, ensure that the member you are trying to access is accessible from the current scope.

3. Why am I getting an 'Invalid use of Member' error in C++?

You may be getting an 'Invalid use of Member' error in C++ due to a mistake in your code, such as using the wrong member access operator or attempting to access a private member. It could also be caused by a problem with your compiler or environment.

4. Can an 'Invalid use of Member' error in C++ be caused by a logical error?

Yes, an 'Invalid use of Member' error in C++ can be caused by a logical error in your code. For example, if you try to access a member of a class before it has been initialized, you may get this error.

5. Is there a way to prevent 'Invalid use of Member' errors in C++?

Yes, there are a few ways to prevent 'Invalid use of Member' errors in C++. First, make sure to use the correct member access operator for the type of member you are trying to access. Also, practice good coding habits, such as properly initializing objects and ensuring that private members are only accessed from within the class.

Similar threads

  • Programming and Computer Science
Replies
2
Views
342
  • Programming and Computer Science
Replies
2
Views
675
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
Back
Top