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.
 
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...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top