C/C++ Problem accessing members of a class in C++?

  • Thread starter Thread starter Whovian
  • Start date Start date
  • Tags Tags
    Class Members
AI Thread Summary
The discussion revolves around a C++ code snippet that defines a class B with a constructor and default constructor. The user encounters a compilation error when trying to access members of an object named Wobbly, which is incorrectly declared as a function instead of an object. The confusion arises from the syntax used in the declaration of Wobbly, specifically the line "B Wobbly ();", which the compiler interprets as a function declaration rather than an object instantiation. The user later realizes this mistake and acknowledges that the issue was due to the compiler misinterpreting the syntax. The expected output of the program is clarified, showing the values of the members for both Wibbly and Wobbly objects.
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 ();
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
Never mind, figured it out! It was in the line

B Wibbly ();

The (compiler?) thought I was declaring a new function.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top