Hi
Please look at this code:
Code:
#include
using namespace std;
class MyClass
{
private:
int d;
public:
int x;
};
int main()
{
MyClass a;
cout << a.x; //gives runtime error: The variable 'a' is being used without being defined.
}
I only added one function
Code:
#include
using namespace std;
class MyClass
{
private:
int d;
public:
int x;
void print(){ cout << d;};
};
int main()
{
MyClass a;
a.print();
cout << a.x; //Now it works
}
But if i write a.print(); after cout << a.x; it gives me run-time error again.
So why is this so?
It says: "a' is being used without being defined." But when i call function it doesnt give error. So is calling function means defining a class?
I do not understand why it gives error first and third but doesnt give error second one.
Can you please help me.
Thanks.