Nope Nope - 2 months ago 6
C++ Question

I have data but it says can not resolve variable "data"

We are learning classes and I am doing my assignment to write a class and 5 different objects and to display the difference.

The professor said that we should use default constructors and the book says this:

A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter).

I am doing exactly what the teacher did; can you please tell me why it says it can't find

#include <iostream>

using namespace std;

class theC
string data;
theC() {

int main()
theC c1();
theC c2();
theC c3();
theC c4();
theC c5(); = "different object 1"; = "different object 2"; = "different object 3"; = "different object 4"; = "different object 5";
cout << << << << <<;
return 0;


Your code has several issues:

Issue one: Is in these lines:

theC c1();  
theC c2();      
theC c3();  
theC c4();  
theC c5();

Here you are trying to declare an instance(object) of your class theC. However, your compiler considers it as a Function Declaration and the Prototype of a function called c1 that takes zero or no parameters(() is empty) and returns an object of type theC.

The correct syntax for declaring an object of your class would : theC c1;

So your Default Constructor will be called as soon as you declare a variable of your class. It is how it works.

Issue two: Also, you can not access any "Private" member. You would need it to be "Public" or you would need to use getters and setters.

Issue three: You are using a the string class without #include <string>.