Board index » delphi » Static data in c++ classes

Static data in c++ classes

Hi Everyone,

I am learning C++. Entered this below program. Using Borland C++ Compiler (5.5.1 for Win32), Linker (5.00). I get errors. Can someone tell me, what exactly is wrong. Thanks in advance, Rajesh.

Error message I get is : Unresolved external 'foo::count'

// program begin

#include <iostream.h>

class foo
{
        private:
                static int count;
        public:
                foo()
                {
                        count++;
                }
                int getcount()
                {
                        return count;
                }

Quote
};

void main()
{
        foo f1, f2, f3;

        cout << "\nCount is " << f1.getcount();
        cout << "\nCount is " << f2.getcount();
        cout << "\nCount is " << f3.getcount();

Quote
}

// program end
 

Re:Static data in c++ classes


On 9 Jun 2003 19:22:54 -0700, "Rajesh" <rvenk...@pacbell.net> wrote in
borland.public.cpp.borlandcpp:

Quote

> Hi Everyone,

> I am learning C++. Entered this below program. Using Borland C++ Compiler (5.5.1 for Win32), Linker (5.00). I get errors. Can someone tell me, what exactly is wrong. Thanks in advance, Rajesh.

First, you are in the wrong newsgroup, although the way they name
these groups does not make it easy to see that.  Your compiler is
supported in news:borland.public.cppbuilder.commandlinetools, since it
is the command line compiler from C++ Builder.

Quote
> Error message I get is : Unresolved external 'foo::count'

> // program begin

> #include <iostream.h>

There has been an ANSI/ISO standard for C++ for almost four years now,
drop the obsolete headers and learn the real language:

#include <iostream>
using namespace std;

Quote
> class foo
> {
>    private:
>            static int count;
>    public:
>            foo()
>            {
>                    count++;
>            }
>            int getcount()
>            {
>                    return count;
>            }
> };

The definition of a class does not actually create or instantiate any
objects.  It does not create any members of the class, and it does not
create the one necessary object of each class static variable.  You
need to define that in a source file outside the class:

int foo::count;

Quote
> void main()

"void main()" has never been legal in C++ (or C either, for that
matter).  Both languages require that main() be defined with a return
type of int in a hosted environment.  If you turn up the compiler
warning levels, it would tell you that.

int main()

Quote
> {
>    foo f1, f2, f3;

>    cout << "\nCount is " << f1.getcount();
>    cout << "\nCount is " << f2.getcount();
>    cout << "\nCount is " << f3.getcount();
> }

> // program end

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Re:Static data in c++ classes


On Mon, 09 Jun 2003 23:35:49 -0500, Jack Klein <jackkl...@spamcop.net>
wrote:

Quote
>On 9 Jun 2003 19:22:54 -0700, "Rajesh" <rvenk...@pacbell.net> wrote in
>borland.public.cpp.borlandcpp:

>> Hi Everyone,

>> I am learning C++. Entered this below program. Using Borland C++ Compiler (5.5.1 for Win32), Linker (5.00). I get errors. Can someone tell me, what exactly is wrong. Thanks in advance, Rajesh.

>First, you are in the wrong newsgroup, although the way they name
>these groups does not make it easy to see that.  Your compiler is
>supported in news:borland.public.cppbuilder.commandlinetools, since it
>is the command line compiler from C++ Builder.

>> Error message I get is : Unresolved external 'foo::count'

>> // program begin

>> #include <iostream.h>

>There has been an ANSI/ISO standard for C++ for almost four years now,
>drop the obsolete headers and learn the real language:

>#include <iostream>
>using namespace std;

>> class foo
>> {
>>        private:
>>                static int count;
>>        public:
>>                foo()
>>                {
>>                        count++;
>>                }
>>                int getcount()
>>                {
>>                        return count;
>>                }
>> };

>The definition of a class does not actually create or instantiate any
>objects.  It does not create any members of the class, and it does not
>create the one necessary object of each class static variable.  You
>need to define that in a source file outside the class:

>int foo::count;

>> void main()

>"void main()" has never been legal in C++ (or C either, for that
>matter).  Both languages require that main() be defined with a return
>type of int in a hosted environment.  If you turn up the compiler
>warning levels, it would tell you that.

>int main()
>> {
>>        foo f1, f2, f3;

>>        cout << "\nCount is " << f1.getcount();
>>        cout << "\nCount is " << f2.getcount();
>>        cout << "\nCount is " << f3.getcount();
>> }

>> // program end

Then again, if you were using the compiler this newsgroup supports,
you would not be as interested in the "newer" headers, right?

Make sure you instantiate your static variable in your cpp as the good
poster mentioned.
Jeff Kish

Re:Static data in c++ classes


Did you really want a static variable in your class? C++'s glib reuse of C
keywords can be a source of confusion. Static has a different meaning in a
class definition, where it defines a sort of "global" variable that is used
by all instances of the class. When used outside a class, it restricts a
variable or function's scope to a single file.

Regards,
Bruce

Other Threads