tier1
tier1

Reputation: 6433

C++ [class] was not declared in this scope while using shared library

I've created a shared library project that does various things with a couple of different classes that I wrote. So my question is more clear, please note the header file below:

TsmClient.h

#include "Headers/Framework/BoostSocket.h"
#include "DataContracts.h"

#ifndef TSMCLIENT_H_
#define TSMCLIENT_H_

using namespace boost;
using namespace boost::asio;

class TsmClient {
public:
    TsmClient();
    TsmClient(std::string host);
    virtual ~TsmClient();

    bool IsConnected();

    friend class WarehouseFramework;

private:
    std::string Communicate(std::string &req);

    std::string serverHost;
    int port;
    std::shared_ptr<BoostSocket> socket;
    io_service service;

    void Connect();
    void Close();
};

By itself, this project builds just fine and produces a .so file. However, when I attempt to use this library in another project, I am having issues accessing a class that I have written in DataContracts.h. The exact error is:

WarehouseSettingTemplateList’ was not declared in this scope

I am able to create an instance of TsmClient and call functions successfully all day long, and since TsmClient.h includes DataContracts.h, I'm a little confused as to why I can't use a class that is declared in DataContracts.h. Does this have to do with the fact that I'm compiling this project as a shared library?

Here is DataContracts.h

    #include "Headers/Framework/Proto/TcpFramework.pb.h"
    #include "Headers/Framework/Proto/ProductManagement.pb.h"
    #include "Headers/Framework/Proto/KioskManagement.pb.h"
    #include "Headers/Framework/Proto/WarehouseManagement.pb.h"
    #include "Headers/Framework/Proto/CashTillManagement.pb.h"
    #include "Headers/Framework/Proto/AccountManagement.pb.h"
    #include "Headers/Framework/Proto/VendorManagement.pb.h"
    #include "Headers/Framework/Proto/ShoppingCartManagement.pb.h"
    #include "Headers/Framework/Proto/Generic.pb.h"
    #include "Headers/Framework/Proto/OfflineManagement.pb.h"
    #include "Headers/Framework/Proto/CouponManagement.pb.h"
    #include "Headers/Utils/Utilities.h"

    using namespace StokedProtoBuf;

    #ifndef DATACONTRACTS_H_
    #define DATACONTRACTS_H_

class WarehouseSettingTemplate {
public:
    long idWarehouse;
    int idWarehouseSettingType;
    std::string Value;
    bool IsEnabled;

    std::string Serialize();
    void Deserialize(const std::string &data);
private:
    WarehouseSettingTemplate_Proto proto;
};

class WarehouseSettingTemplateList {
public:
    std::vector<WarehouseSettingTemplate> settingList;
    std::string Serialize();
    void Deserialize(const std::string &data);
private:
    WarehouseSettingTemplateList_Proto proto;
};

I'm sure this is a stupid oversight on my part but it is driving me insane.

Upvotes: 0

Views: 1608

Answers (2)

tier1
tier1

Reputation: 6433

Hate to answer my own question after all of the help that I've received, but here is what was wrong...

I had a header file in my shared library called DataContracts.h. In my main project, I also had a header file called DataContracts.h

So I think that my include guards where preventing the shared library header file to be imported. This is the first time that I've really worked with shared libraries before so I hope that this will help other people if they run into this issue.

--I feel bad because I did not provide enough information for anybody else to answer this question.

Upvotes: 1

char8_t
char8_t

Reputation: 320

First its better that you have your header guard (#ifndef/#define) on the top of your file (above your includes)

Second, make sure that every class is available in your .so and the program you linking against.

Third, You should not use inline declarations in your classes which will be exported otherwise at compile time the linker cannot choose which definition to use (the definition in your .so file or your program).

Upvotes: 0

Related Questions