1

I'm using xcode 4.2 to build this simple program. I realize there are a lot of post on this error but I haven't found any that answer my problem.
I am getting an error that I do not understand.
Here is the compilation output:

Ld /Users/kotoko/Library/Developer/Xcode/DerivedData/stw-gyleohvghcrywgcqkihhkkkqeqnl/Build/Products/Debug/stw normal x86_64 cd /Users/kotoko/projectos/somethingToWear/stw_v6_xcode/stw setenv MACOSX_DEPLOYMENT_TARGET 10.6 /Developer/usr/bin/llvm-g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -

L/Users/kotoko/Library/Developer/Xcode/DerivedData/stw-gyleohvghcrywgcqkihhkkkqeqnl/Build/Products/Debug -F/Users/kotoko/Library/Developer/Xcode/DerivedData/stw-gyleohvghcrywgcqkihhkkkqeqnl/Build/Products/Debug -filelist /Users/kotoko/Library/Developer/Xcode/DerivedData/stw-gyleohvghcrywgcqkihhkkkqeqnl/Build/Intermediates/stw.build/Debug/stw.build/Objects-normal/x86_64/stw.LinkFileList -mmacosx-version-min=10.6 -o /Users/kotoko/Library/Developer/Xcode/DerivedData/stw-gyleohvghcrywgcqkihhkkkqeqnl/Build/Products/Debug/stw

Undefined symbols for architecture x86_64: "ClosetItem::lc", referenced from: ClosetItem::ClosetItem(int)in ClosetItem.o ClosetItem::ClosetItem(int)in ClosetItem.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status

Here is the code (the main file doesn't even call this objects for now):

//
//  ClosetItem.h
//  stw
//

#ifndef stw_ClosetItem_h
#define stw_ClosetItem_h

#include <iostream>

class LeakChecker { 
    int count;
public: 
    LeakChecker() : count(0) {}
    void print() { 
        std::cout << count << std::endl;
    } 
    ~LeakChecker() { print(); } 
    void operator++(int) { count++; } 
    void operator--(int) { count--; }
};

class ClosetItem{

public:
    ClosetItem(int identifier);
    virtual ~ClosetItem() {};


protected:
    static LeakChecker lc;
};
#endif
//
//  ClosetItem.cpp
//  stw
//

#include "ClosetItem.h"
#include <iostream>

ClosetItem::ClosetItem(int identifier){
    lc++;
    std::cout<<"ClosetItem #";
    lc.print();
}

Can anyone point me out the problem please?

kotoko
  • 599
  • 2
  • 6
  • 22

3 Answers3

8

You haven't initialized your static member:

class ClosetItem{

public:
    ClosetItem(int identifier);
    virtual ~ClosetItem() {};


protected:
    static LeakChecker lc;  // <-- uninitialized
};

You need to initialize it in an implementation file:

//ClosetItem.cpp
LeakChecker ClosetItem::lc; // <-- definition
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

You have declared a static LeakChecker variable but need to implement it. In your c++ file add:

LeakChecker ClosetItem::lc;

RobH
  • 3,199
  • 1
  • 22
  • 27
1

You've not actually defined the lc static object. You need something like:

LeakChecker ClosetItem::lc;
Component 10
  • 10,247
  • 7
  • 47
  • 64