|
While working with static class i got this error.
Linking...
FTP.obj : error LNK2001: unresolved external symbol "public: static struct HWND__
* FTP::hFTPWind" (?hFTPWind@FTP@@2PAUHWND__@@A)
LeedharFTP.obj : error LNK2001: unresolved external symbol "public: static struct
HWND__ * FTP::hFTPWind" (?hFTPWind@FTP@@2PAUHWND__@@A)
Debug/LeedharFTP.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
LeedharFTP.exe - 3 error(s), 0 warning(s)
This is how I fixed it
The problem was when u declare a member variable as static u have to intitlize
it globally before using it . Here's some info i got from this site.
http://www.functionx.com/vcnet/keywords/static.htm
In C++, you must first declare a variable before using it. The C++ language provides an
exception to this rule through the static keyword. If you declare a member variable as
static in a class, an instance of that class would be made available when the class is used.
If you declare a member variable of a class as static, in C++, you must initialize it
globally before using the static member variable. Here is an example:
#include <iostream>
using namespace std;
class CSquare
{
public:
static double Side;
CSquare() {};
void setSide(double S) { Side = S; }
double getSide() { return Side; }
double Area() { return Side * Side; }
};
double CSquare::Side = 30.65;
int main()
{
// TODO: Please replace the sample code below with your own.
CSquare Sqr;
cout << "Square Characteristics";
cout << "nSide: " << CSquare::Side;
cout << "nArea: " << Sqr.Area() << endl;
Sqr.setSide(44.28);
cout << "nSquare Characteristics";
cout << "nSide: " << CSquare::Side;
cout << "nArea: " << Sqr.Area();
cout << "n";
return 0;
}
This would produce:
Square Characteristics
Side: 30.65
Area: 939.422
Square Characteristics
Side: 44.28
Area: 1960.72
Press any key to continue
Notice that, in the above class, we declared the static member variable public, which is
sometimes considered bad programming. If you decide to "hide" a member variable in a
private section, then you should create a method that would allow the "external world"
to access the member variable.
Besides a static method that serves as intermediary between a static member variable and other
classes, you can use as many methods as you judge necessary. Here is an example:
#include <iostream>
using namespace std;
class CSquare
{
private:
static double Side;
public:
CSquare() {};
static void setSide(double S) { Side = S; }
static double getSide() { return Side; }
static double Area() { return Side * Side; }
};
double CSquare::Side = 30.65;
int main()
{
// TODO: Please replace the sample code below with your own.
CSquare Sqr;
cout << "Square Characteristics";
cout << "nSide: " << CSquare::getSide();
cout << "nArea: " << Sqr.Area() << endl;
Sqr.setSide(44.28);
cout << "nSquare Characteristics";
cout << "nSide: " << CSquare::getSide();
cout << "nArea: " << Sqr.Area();
cout << "n";
return 0;
}
The result is the same as above
|
|