Problem accessing members of a class in C++?

  • Context: C/C++ 
  • Thread starter Thread starter Whovian
  • Start date Start date
  • Tags Tags
    Class Members
Click For Summary
SUMMARY

The forum discussion addresses a common issue in C++ regarding object instantiation and function declaration. The user encounters an error when attempting to access members of an object due to the line "B Wibbly ();", which the compiler interprets as a function declaration instead of an object instantiation. The solution is to remove the parentheses when declaring the object to avoid confusion with function prototypes, allowing for proper access to the object's members.

PREREQUISITES
  • Understanding of C++ class syntax and member access
  • Familiarity with object instantiation in C++
  • Knowledge of function declaration syntax in C++
  • Basic debugging skills in C++ programming
NEXT STEPS
  • Review C++ object instantiation rules and best practices
  • Learn about constructor overloading in C++
  • Explore common C++ compiler errors and their resolutions
  • Study the differences between function declarations and object instantiation
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting object-oriented programming issues in C++.

Whovian
Messages
651
Reaction score
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 ();
count << "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
Never mind, figured it out! It was in the line

B Wibbly ();

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

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
89
Views
7K