简体   繁体   中英

Undefined symbols for architecture x86_64 when compiling c++

I am continuously getting that error, and it's driving me crazy!

Undefined symbols for architecture x86_64:
  "SSResourcesDepot::_sharedInstance", referenced from:
    SSResourcesDepot::sharedInstance() in SSResourcesDepot.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

My code looks like:

#ifndef SolarSystem_SSResourcesDepot_h
#define SolarSystem_SSResourcesDepot_h
#include "SSResource.h"

/* SSResourcesDepot is implemented as a Singleton Depot that keeps track of all
 * requested Resource objects, and avoid loading them twice in memory. */
class SSResourcesDepot {

    SSResourcesDepot() {};
    SSResourcesDepot(SSResourcesDepot const&){};
    SSResourcesDepot& operator=(SSResourcesDepot const&){};

    static SSResourcesDepot* _sharedInstance;

    SSResource* _search(std::string resourceName);
    SSResource* _load(std::string resourceName);

public:
    static SSResourcesDepot* sharedInstance();
    SSResource* requestResource(std::string resourceName);

};

#endif

and:

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


#pragma mark Public methods
SSResourcesDepot* SSResourcesDepot::sharedInstance() {
    if (SSResourcesDepot::_sharedInstance == 0) {
        SSResourcesDepot::_sharedInstance = new SSResourcesDepot();
    }
    return SSResourcesDepot::_sharedInstance;
}

SSResource* SSResourcesDepot::requestResource(std::string resourceName) {
    SSResource *resource = this->_search(resourceName);
    if (resource == NULL) resource = this->_load(resourceName);
    return resource;
}


#pragma mark Private methods
SSResource* SSResourcesDepot::_search(std::string resourceName) {
    return NULL;
}

SSResource* SSResourcesDepot::_load(std::string resourceName) {
    return NULL;
}

It seems completely functional to me, but Apple-O-Matcher keeps complaining, and it doesn't let me compile ... :-S

Thanks in advance!

You didn't initialize your static member.

Add

SSResourcesDepot* SSResourcesDepot::_sharedInstance = NULL;

to your implementation file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM