-
December 21st, 2012, 10:26 AM
#1
Defining a constant datamember
Programe #1
// file.h
class File
{
public:
static const int var = 9;
};
-
// main.cpp
#include <iostream>
#include "file.h"
using namespace std;
int main() {
File f;
cout << f.var;
return 0;
}
Programe #2
// file.h
int Globalvar ;
class File
{
public:
static const int var = 9;
};
-
// main.cpp
extern int GlobalVar;
#include <iostream>
#include "file.h"
using namespace std;
int main() {
cout << GlobalVar;
return 0 ;
}
Program#1 is running fine, but program#2 gives linker error:
error LNK2005: "int GlobalVar" (?x@@3HA) already defined in file.obj
I know the header files are never compiled. Then in the above case, how the compiler knows the definition of variable var, but not able to find the definition of GlobalVar? What is the difference between this two programs?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|