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

  • Thread starter Thread starter lewis198
  • Start date Start date
  • Tags Tags
    C++ Error Member
AI Thread Summary
The discussion revolves around issues encountered while defining and using a class in C++. The user successfully declares a class with public members and member functions that compute values based on these members. However, errors arise when trying to assign values to the members and call a member function in the main program. Key points include the need to instantiate the class correctly and assign values to its members using the object reference (e.g., x.member1=value). Additionally, the correct way to call a member function is by using parentheses (e.g., printf("%d", x.member_function())). It is also noted that a semicolon is required after the class declaration, and members must be declared as public to be accessible outside the class.
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
22
Views
3K
Replies
35
Views
4K
Replies
8
Views
2K
Replies
25
Views
2K
Replies
25
Views
3K
Back
Top