I am writing a program in C++ using my own header file.
main.cpp
#include<iostream>
#include"operation.h"
using namespace std;
main()
{
int a;
cout <<"Enter the value a";
cin>>a;
//class name add
//obj is object of add
add obj;
obj.fun(a);
}
operation.h
class add
{
void fun(int b)
{
int c,d=10;
c=d+b;
cout<<"d="<<d;
}
}
When I compile using G++ in Linux, it is showing the following errors:
->expected ";" before obj
->obj not declared in this scope
How do I solve this problem? Why is this happening?
Best Solution
You need to add
public:
at the top of class add. The default for class members is to make them private.Also, you're missing a semicolon at the end of the class definition. C++ requires class definitions to end with a semicolon following the closing curly-brace (you could actually declare a variable at that point).