tags:

views:

992

answers:

5

I am wrtting 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++ compiler in linux it is showing error: expected ";" before obj ->obj not decleared in this scope

I don't understand this error, can someone help me?

thank you

+6  A: 

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).

Mark Ransom
Thanks for fixing my answer, Shog9. I've been bitten by the missing semi-colon myself, it's an easy thing to miss.
Mark Ransom
@Mark: no problem - caught it while editing, and it seemed like a good fit with your answer.
Shog9
A: 

To me it looks like that is not the only problem you have got:

$ g++ -c /tmp/ttt.cpp
In file included from /tmp/ttt.cpp:2:
/tmp/operation.h: In member function ‘void add::fun(int)’:
/tmp/operation.h:8: error: ‘cout’ was not declared in this scope
/tmp/ttt.cpp: At global scope:
/tmp/ttt.cpp:4: error: expected unqualified-id before ‘using’
/tmp/ttt.cpp: In function ‘int main()’:
/tmp/ttt.cpp:8: error: ‘cout’ was not declared in this scope
/tmp/ttt.cpp:9: error: ‘cin’ was not declared in this scope
/tmp/operation.h:4: error: ‘void add::fun(int)’ is private
/tmp/ttt.cpp:14: error: within this context

Make sure you address all of the errors in the header file first.

lothar
+1  A: 

I fail to see the real reason for the error, but you should really insert include guards and there is a missing ; after the class definition:

// operation.h
#ifndef OPERATION_H
#define OPERATION_H
class add {
public:
   void fun( int b ) {
      int c = 0; // always initialize, just in case
      int d = 10;
      c = d+b;
      std::cout << "d=" << d;
   }
};
#endif

EDIT: main() return type is not present. You should add a 'int' at that point.

David Rodríguez - dribeas
how do you fail to see reason,but then you fix it?
TStamper
I had not found the solution to the compiler error. The initial code has two small problems corrected, but none of them should trigger that compiler error: header guards are not required, but in a more complex scenario will save you from double definitions. Making the fun() method public will correct the access limitation error _after_ the real problem is solved.
David Rodríguez - dribeas
Or so I thought... c++ compiler errors are sometimes a little confusing.
David Rodríguez - dribeas
+1  A: 

Hi :)

As mentioned above first "wrongness" is missing semicolon after class bracket; second is private access violation (make this method public),

My suggestion (to keep your header file "clean") put definition of your class method into *.cpp file and let your header contain only declaration (you will avoid unnecessary inclusion of iostream header.)

so *.hpp file should contain only:

class add { public: void fun( int b ); };

and *.cpp

void add::fun( int b) { //here goes implementation }

so good luck in coding:)

A: 

How are you initializing the class? Note that the following is the correct way to instantiate a new "add" object in C++:

add a(params);

Which is not equivalent to the following:

add(params) a;

Subtle difference, and definitely a departure from the way some other languages do it. Could conceivably generate the "expected ;" error.

As an aside, I agree with above commenter that classes should be capitalized and functions not. This is a convention that makes for much greater readability, and which you will see repeated in other OO languages. OTOH, this assumes readable code as your goal. Whether to name using CamelCase or underscores is more subjective, and thus more contentious, IMHO.

Good luck,

Noah

Noah