Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Undeclared identifier in API

Undeclared identifier in API

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++jsonquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    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.

    C O 2 Replies Last reply
    0
    • P piul

      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.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      What's wrong in including param.h in MyApiClass source file?

      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

      P 1 Reply Last reply
      0
      • C CPallini

        What's wrong in including param.h in MyApiClass source file?

        THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

        P Offline
        P Offline
        piul
        wrote on last edited by
        #3

        well... the application in which I use the lib won't compile, will it?? Or am I missing something? Including param.h in libApi.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"

        1 Reply Last reply
        0
        • P piul

          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.

          O Offline
          O Offline
          Orjan Westin
          wrote on last edited by
          #4

          You can use the pimpl idiom:

          //libApi.h

          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...)

          F 1 Reply Last reply
          0
          • O Orjan Westin

            You can use the pimpl idiom:

            //libApi.h

            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...)

            F Offline
            F Offline
            Freak30
            wrote on last edited by
            #5

            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.

            O 1 Reply Last reply
            0
            • F Freak30

              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.

              O Offline
              O Offline
              Orjan Westin
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups