GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-May-2005, 23:57
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road

error related to static member functions and varaibles


CPP / C++ / C Code:
#include<iostream.h>
class static_type{
	static int i;
public:
	static void init(int x){i=x;}
	void show(){cout<<i;}
};
main()
{

	static_type::init(5);
	static_type a;
	a.show();
	return 0;
}
im compiling with VC++ ,here's the error i get:

error LNK2001: unresolved external symbol "private: static int static_type::i" (?i@static_type@@0HA)
Debug/a.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

what should i do, usually when i get this type of error i just close Vc++ and then re-enter and compile the program again and ... thats about it ! i don't get the error again, but with this one ... doesn't work.
need ur help fast.
thanks kai85.
  #2  
Old 20-May-2005, 02:47
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 918
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
You need to "declare" your static variables in exactly one place before using your class:

CPP / C++ / C Code:
class static_type{
	static int i;
public:
	static void init(int x){i=x;}
	void show(){cout<<i;}
};

int static_type::i;

You can initialise it to a value in the same place if you need to.

Quote:
Originally Posted by kai85
what should i do, usually when i get this type of error i just close Vc++ and then re-enter and compile the program again and ... thats about it ! i don't get the error again, but with this one ... doesn't work.



You should send this to Microsoft! I wonder what they would say to that complaint!

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 20-May-2005, 04:43
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road
Quote:
Originally Posted by LuciWiz
You need to "declare" your static variables in exactly one place before using your class:

CPP / C++ / C Code:
class static_type{
	static int i;
public:
	static void init(int x){i=x;}
	void show(){cout<<i;}
};

int static_type::i;

You can initialise it to a value in the same place if you need to.





You should send this to Microsoft! I wonder what they would say to that complaint!

Best regards,
Lucian
ok i did that , no more errors .
but why?
why do i need to declare istatic varables in exactly one place before i use them ? and by this exactly one place do u mean before the main function?
thanks kai85.
  #4  
Old 20-May-2005, 06:11
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by kai85
why do i need to declare istatic varables in exactly one place before i use them ? and by this exactly one place do u mean before the main function?
thanks kai85.

Because if you declare it more than one place you will anger the little imps that live in your compiler and they will refuse to work. They will claim confusion and direct you to their union rep.
Well, maybe not exactly but it paints a pretty picture, don't it?

Using the code example you can see how Luci seperated your declaration of your static integer in the definition of your class:
CPP / C++ / C Code:
class static_type{
  static int i;
public:
  static void init(int x){i=x;}
  void show(){cout<<i;}
};

from your definition in the implementation of the class member variable.
CPP / C++ / C Code:
int static_type::i;

You could do a number of things.
  • Include the whole thing in your main code file
  • Have a seperate file for your class containing the above code
  • Have a class header file with the definition of the class and an implementation file for your class that would contain the definition of your static member (as well as any other code you may have). That would be a .h and .cpp file respectively.

For the first one it would look like:
CPP / C++ / C Code:
#include <iostream>
using std::cout;
using std::endl;

class static_type{
  static int i;
public:
  static void init(int x){i=x;}
  void show(){cout<<i;}
};

int static_type::i;

int main(){
  // a useless test program
  static_type my_static_int;
  my_static_int.init(100);
  cout << "My static int is ";
  my_static_int.show();
  cout << endl;
  return 0;
}

The second one also uses inclusion guards on the header file as good practice.
CPP / C++ / C Code:
#include "static_int.h"
#include <iostream>
using std::cout;
using std::endl;

int main(){
  // a useless test program
  static_type my_static_int;
  my_static_int.init(100);
  cout << "My static int is ";
  my_static_int.show();
  cout << endl;
  return 0;
}
and your header
CPP / C++ / C Code:
#ifndef STATIC_INT_H
#define STATIC_INT_H

#include <iostream>
using std::cout;

class static_type{
  static int i;
public:
  static void init(int x){i=x;}
  void show(){cout<<i;}
};

int static_type::i;
#endif

Finally the third example from my list which is good when things start getting larger and more complex.
CPP / C++ / C Code:
// main.cpp
#include "static_int.h"
#include <iostream>
using std::cout;
using std::endl;

int main(){
  // a useless test program
  static_type my_static_int;
  static_type your_static_int;

  my_static_int.init(100);
  cout << "My static int is ";
  my_static_int.show();
  cout << endl;

  your_static_int.init(200);
  cout << "My static int is ";
  my_static_int.show();
  cout << endl;
  cout << "Your static int is ";
  my_static_int.show();
  cout << endl;

  return 0;
}
CPP / C++ / C Code:
// static_int.h
#ifndef STATIC_INT_H
#define STATIC_INT_H

#include <iostream>
using std::cout;

class static_type{
  static int i;
public:
  static void init(int x){i=x;}
  void show(){cout<<i;}
};
#endif
CPP / C++ / C Code:
// static_int.cpp
#include "static_int.h"

int static_type::i;

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.