Murugan.com
Murugan Andezuthu Dharmaratnam

  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  PHP   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  



VC++ LNK2001: unresolved external symbol public: static in static class


Home  > VC++  > VC++ LNK2001: unresolved external symbol public: static in static class  
       

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
.Net VC File IO Sample Code

activex mdb

com smartcard

COM variant Data type

DirectX Store Video as mpeg

GetLastError() in VC++ Win32 Display the Value in a MessageBox

GetWindowName() VC++ Win32 Get a Window Name Given Handle

index

multi threaded socket

shellexecute open internet explorer

VC Win32 Tree View Control Change Background color of tree

VC++ 6.0 Win32 SetParent () Add an Existing Window as child window of another or Dynamically add a child window or Assign Window to a parent

VC++ 6.0 Win32 Splitter control like SplitContainer in .Net

VC++ 6.0 Win32 What are the Different Windows Controls That are Available in Win32 Non MFC

VC++ DWORD To String

VC++ Error LNK2001 unresolved external symbol public static

VC++ Win32 compare string with empty character constant

VC++ Win32 Compress Video To Mpeg

VC++ Win32 Exit Application

VC++ Win32 FTP Sessions Client Application

VC++ WIn32 IP from domain name

VC++ win32 Listview Get Selected Item

VC++ win32 ListView or LIST Control how to scroll to a list or make sure that a list item is visible

VC++ Win32 ListView SelectedItem()

VC++ Win32 Open in Internet explorer or firefox

Does Windows have an Inbuild FileExist function API



  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  PHP   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  

Copyright 2009 @ Murugan Andezuthu Dharmaratnam