0

Possible Duplicate:
What is the difference between a definition and a declaration?

In my A.h file:

extern int a,b;
private: int x,y;
public: A():x(1),y(2){}
void total();

In my B.h file:

private: int a,b;
public: A():a(1),b(2){}

In my C.cpp file:

#include "A.h"
void total(){
int total = a + b + x + y;}

When I build the program, It said that :

undefined reference to `a';
undefined reference to `b';

Can anybody tell me why ? Thank you.

Community
  • 1
  • 1
Xitrum
  • 7,283
  • 25
  • 83
  • 118
  • 1
    Please post an example that actually compiles and reproduces the error. – Björn Pollex Mar 09 '11 at 13:57
  • 1
    Well the problem is that it won't compile ;) – RvdK Mar 09 '11 at 13:58
  • 1
    What exactly are you trying to do with those `public` and `private` keywords? They only make any sense inside a class. You don't have them inside a class. – T.E.D. Mar 09 '11 at 14:03
  • Is that all your code? This seems strange cause there is no struct declaration or class declaration to which the public/private members would belong. BTW how do you compile your code? – M'vy Mar 09 '11 at 14:05
  • `extern` turns a [definition into a declaration](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/). Look at the linked question. Yours is a dupe of it. – sbi Mar 09 '11 at 14:05
  • in A.h `extern int a,b;` declares two global variables but doesn't allocate any space for them. In B.h `public: int a,b` defines two member variables - public only applies to member visibility. No where do you define the global variables a and b - this should be done in a source file not a header as the variables may be defined wherever the header is included. – DanS Mar 09 '11 at 14:57

0 Answers0