Undeclared identifier in API
-
Hi all, I'm working on a library of which I only want to distribute the .lib and a .h. The header defines the interface or api to the library, the idea being that the user does not bother much about the insides of it. However, I have a concept issue.
//libApi.h
#include #include class MyApiClass{
public:
//some methods...
private:
// var map
std::map vars;
};Obviously this throws an error saying Param is not defined when I compile another application that includes
libApi.h
and links the library. The thing is, the class Param is declared in Param.h and defined in Param.cpp, files that I don't want the user to play with. What are my options?? Thanks in advance. paul. -
Hi all, I'm working on a library of which I only want to distribute the .lib and a .h. The header defines the interface or api to the library, the idea being that the user does not bother much about the insides of it. However, I have a concept issue.
//libApi.h
#include #include class MyApiClass{
public:
//some methods...
private:
// var map
std::map vars;
};Obviously this throws an error saying Param is not defined when I compile another application that includes
libApi.h
and links the library. The thing is, the class Param is declared in Param.h and defined in Param.cpp, files that I don't want the user to play with. What are my options?? Thanks in advance. paul.What's wrong in including
param.h
inMyApiClass
source file?THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
What's wrong in including
param.h
inMyApiClass
source file?THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
well... the application in which I use the lib won't compile, will it?? Or am I missing something? Including
param.h
inlibApi.cpp
, when compiling the application//MyApp.cpp
#include "libApi.h"
//library links with this
void main (void)
{
//...
}I get an error saying "libApi.h: 'Param' undeclared identifier"
-
Hi all, I'm working on a library of which I only want to distribute the .lib and a .h. The header defines the interface or api to the library, the idea being that the user does not bother much about the insides of it. However, I have a concept issue.
//libApi.h
#include #include class MyApiClass{
public:
//some methods...
private:
// var map
std::map vars;
};Obviously this throws an error saying Param is not defined when I compile another application that includes
libApi.h
and links the library. The thing is, the class Param is declared in Param.h and defined in Param.cpp, files that I don't want the user to play with. What are my options?? Thanks in advance. paul.You can use the pimpl idiom:
class MyApiClass{
public:
//some methods, the implementation of which is simply forwarding to MyApiClassImpl ...
private:
// forward declaration of implementation details
class MyApiClassImpl;
//
std::auto_ptr<MyApiClassImpl> impl;
};and
// libApi.cpp
#include <map>
#include <string>
#include "Param.h"// Declaration of implementation class
class MyApiClass::MyApiClassImpl
{
public:
// Mirroring MyApiClass...
private:
std::map vars;
};// Implementation of API class
MyApiClass::MyApiClass()
: impl(new MyApiClassImpl())
{}int MyApiClass::SomeFunction()
{
return impl->SomeFunction();
}// Implementation class
MyApiClass::MyApiClassImpl::MyApiClassImpl()
{}int MyApiClass::MyApiClassImpl::SomeFunction()
{
// Do the stuff...
}(Edited to fix code written too fast...)
-
You can use the pimpl idiom:
class MyApiClass{
public:
//some methods, the implementation of which is simply forwarding to MyApiClassImpl ...
private:
// forward declaration of implementation details
class MyApiClassImpl;
//
std::auto_ptr<MyApiClassImpl> impl;
};and
// libApi.cpp
#include <map>
#include <string>
#include "Param.h"// Declaration of implementation class
class MyApiClass::MyApiClassImpl
{
public:
// Mirroring MyApiClass...
private:
std::map vars;
};// Implementation of API class
MyApiClass::MyApiClass()
: impl(new MyApiClassImpl())
{}int MyApiClass::SomeFunction()
{
return impl->SomeFunction();
}// Implementation class
MyApiClass::MyApiClassImpl::MyApiClassImpl()
{}int MyApiClass::MyApiClassImpl::SomeFunction()
{
// Do the stuff...
}(Edited to fix code written too fast...)
Your Approach only works as long as you are never required to pass a Param object as paramter or return one. Of course you could work with forward declaration (if you only pass references/pointers), but I would suggest another solution. Create a libapi.hpp and include the headers in the correct order (i.e. map, string, Param.h, libApi.h). Consumers of your API inlclude the hpp. In the libApi.cpp you include the Param.h before the libApi.h. Then it should work.
The good thing about pessimism is, that you are always either right or pleasently surprised.
-
Your Approach only works as long as you are never required to pass a Param object as paramter or return one. Of course you could work with forward declaration (if you only pass references/pointers), but I would suggest another solution. Create a libapi.hpp and include the headers in the correct order (i.e. map, string, Param.h, libApi.h). Consumers of your API inlclude the hpp. In the libApi.cpp you include the Param.h before the libApi.h. Then it should work.
The good thing about pessimism is, that you are always either right or pleasently surprised.
Ideally, you'd want minimal exposure, to ensure low coupling. The original post made a point of only showing Param in the private section, and said the user of the library should not be playing around with Param, so I assumed it was not a public type. If the Param type was a public type used in the interface, the best solution would be to include Param.h in libApi.h instead of introducing a new file.