Problem accessing members of a class in C++?

I just changed it toB Wibbly;In summary, the issue was that the line "B Wibbly ();" was mistakenly declaring a new function instead of creating a new object. This was resolved by changing it to "B Wibbly;" to properly create a new object.
  • #1
Whovian
652
3
#include <iostream>
using namespace std;

class B
{
public:
int a,b;
string c;
B (int, int, string);
B ();
};

B::B (int d, int e, string f)
{
a = d;
b = e;
c = f;
}

B::B ()
{
a = 0;
b = 0;
c = "Nothing";
}

int main()
{
B Wibbly (1,2,"Wibbly");
B Wobbly ();
cout << "Wibbly: " << Wibbly.a << " " << Wibbly.b << " " << Wibbly.c << endl << "Wobbly: " << Wobbly.a << " " << Wobbly.b << " " << Wobbly.c << endl;
return 0;
}

For some reason, I get an error "error: request for member 'a' in 'Wobbly', which is of non-class type 'B ()()'" (and the same for b and c). Could anyone tell me what I'm doing wrong? I'm expecting

Wibbly: 1 2 Wibbly
Wobbly: 0 0 Nothing

EDIT: I now know that this wasn't the appropriate forum. Still, could someone please help?
 
Last edited:
Technology news on Phys.org
  • #2
Never mind, figured it out! It was in the line

B Wibbly ();

The (compiler?) thought I was declaring a new function.
 

Related to Problem accessing members of a class in C++?

1. How do I access private members of a class in C++?

C++ provides access modifiers to control the visibility of class members. By default, class members are private and can only be accessed within the class. To access private members outside the class, you can use public member functions or make them friend functions.

2. Can I access protected members of a class in C++?

Yes, protected members can be accessed by the class itself, its derived classes, and friend functions. They are similar to private members, but can also be accessed by derived classes.

3. What is the difference between public, private, and protected members in C++?

Public members can be accessed by any code that has access to the class. Private members can only be accessed by the class itself and friend functions. Protected members can be accessed by the class, its derived classes, and friend functions.

4. How do I declare a friend function in C++?

To declare a friend function, you need to add the keyword "friend" before the function declaration in the class definition. This allows the function to access private and protected members of the class.

5. Can I access class members without creating an object in C++?

No, you must create an object of the class to access its members. Static members can be accessed without creating an object, but they are not tied to a specific instance of the class.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
766
  • Programming and Computer Science
2
Replies
35
Views
3K
  • Programming and Computer Science
2
Replies
36
Views
4K
  • Programming and Computer Science
Replies
2
Views
724
  • Programming and Computer Science
Replies
1
Views
908
  • Programming and Computer Science
Replies
4
Views
831
Back
Top