delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/22/13:59:12

From: Tero Parvinen <tero AT mnar DOT tky DOT hut DOT fi>
Newsgroups: comp.os.msdos.djgpp
Subject: Undefined reference to static members
Date: Mon, 21 Oct 1996 23:11:48 +0200
Organization: Helsinki University of Technology
Lines: 57
Distribution: inet
Message-ID: <326BE714.3019E999@mnar.tky.hut.fi>
NNTP-Posting-Host: mnar.tky.hut.fi
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I can't get djgpp to recognize static member variables. If I do a 
'gcc -c file.cc' everything seems to run fine, but if I try to link the
object files, I get messages complaining about undefined references.

Here's a piece of code to illustrate the problem:

---CUT---
#include <stdio.h>

class CTest
{
 public:
    static int a;
    static void SSetA(int p);
    void SetA(int p);
    int GetA();
};

void CTest::SSetA(int p)
{
    a = p;
}

void CTest::SetA(int p)
{
    a = p;
}

int CTest::GetA()
{
    return a;
}

int main(int argc, char *argv[])
{
    CTest::SSetA(1);
    
    CTest test;
    printf("%u\n", test.GetA);
    test.SetA(10);
    printf("%u\n", test.GetA);
}
---END CUT---

the output for
'gcc stest.cc' is:

stest.cc(.text+0x7): undefined reference to `CTest::a'
stest.cc(.text+0x23): undefined reference to `CTest::a'
stest.cc(.text+0x3b): undefined reference to `CTest::a'

What's wrong here? Is it a bug or am I doing something wrong?

--
Tero Parvinen
Tero DOT Parvinen AT hut DOT fi
http://www.niksula.cs.hut.fi/~tparvine

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019