The
extern
keyword is used when you are using global variables. You never actually use it, though, because you never actually use global variables.
static
has two uses - if you see a static function and it is not part of a class, then it was probably written before the use of
static like this was deprecated. It means the same thing as writing the function inside an anonymous namespace; it is only visible to that source file and no others.
If you see a
static
member function or member variable in a class, then that means it is associated with the class and not any one instance. You can access it with
NameOfClass::NameOfMember.
All other members in a class are non-
static by default, which means they are associated with specific instances of the class. You access them with
name_of_instance.NameOfMember.
As for the errors and warnings you are getting, look at the actual lines your compiler is telling you about:
C:\C++\StaticVariable\Calc1.cpp||In function 'void Sub1()':|
C:\C++\StaticVariable\Calc1.cpp|24|warning: variable 'z' set but not used [-Wunused-but-set-variable]|
C:\C++\StaticVariable\Calc1.cpp|26|warning: 'x' is used uninitialized in this function [-Wuninitialized]|
C:\C++\StaticVariable\Calc1.cpp|26|warning: 'y' is used uninitialized in this function [-Wuninitialized]| |
53 54 55 56 57 58 59 60 61 62
|
void Sub1(){
int x; //declared, not initialized
int y; //declared, not initialized
int z; //declared, not inisialized
z = x+y; //using x and y which have not been initialized
//not using z despite setting it
}
|
Maybe you meant to implement
void Calc1::Sub1(){}
? And the same for
void Calc1::Sub2(int Num){}
C:\C++\StaticVariable\main.cpp|10|multiple definition of `Data1::StringName'|
obj\Debug\Calc1.o:C:\C++\StaticVariable\Calc1.cpp|19|first defined here| |
Not sure about this, I can't see it in your code. Maybe you were trying to use a global variable and forgot to fully erase it when you realized how bad they are?
obj\Debug\main.o:main.cpp|| undefined reference to `Calc1::Sub2(int)'| |
You never define
Calc1::Sub2
, see above.