Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 19th, 2003, 03:33 AM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default A problem with static data members in c++

When I run the following code in vc++ I get linker error 2001.
What is the problem and what can be done?
the code:

class a{
protected:static int aa;
public:void doo();
};
void a::doo(){aa=5;}
void main(){}


Reply With Quote
  #2 (permalink)  
Old August 19th, 2003, 10:49 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

//a.h

class a
{
protected:
    static int aa;//declares the static variable

public:
    void a::doo();
    void display();

};
--------------------------

//a.cpp
#include "test.h"
#include<iostream>

using namespace std;

void a::doo()
{
    aa=5;
}

void a::display()
{
    cout<<aa<<endl;
}

int a::aa = 0;//defines and initializes the static member

//aa exists before any objects of the class exist
//so it cannot be initialized in a class function.
--------------------
//main.cpp
#include<iostream>
#include "test.h"

using namespace std;

int main()
{
    a A;
    A.display();
    A.doo();
    A.display();


    return 0;
}
Reply With Quote
  #3 (permalink)  
Old August 20th, 2003, 02:22 AM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much 7stud, for the detailed answer. You've been most helpful. Thank you.
Amots
Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with static variables behaviour shujamughal ASP.NET 2.0 Professional 4 July 21st, 2007 01:56 AM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
"Static data and pivot" paul20091968 Excel VBA 1 June 26th, 2006 03:45 PM
Callin Static Members from .NET Dll in VBA syoguran VB How-To 0 March 13th, 2006 04:37 AM
Static members global to client apps Jnye ASP.NET 1.0 and 1.1 Professional 0 March 16th, 2004 01:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.