need help in maintaining a project

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rex_chaos

    need help in maintaining a project

    Hi all,
    I am writing a project in C++. I am going to compile all my source
    to a library(.a). So I can only distribute the lib as well as the
    header file to my client. However, I don't quite familiar with C++
    programming. In my project, I have defined a heap of classes, the
    definition of which defined in a hearder file. On the other hand, I
    would like to limit all the classes in a namespace.

    Can anyone please tell me how to write the header file? Please give me
    an simple example.

    Any reply will be highly appreciated!
  • Mike Wahler

    #2
    Re: need help in maintaining a project


    "Rex_chaos" <rex_chaos@21cn .com> wrote in message
    news:f7a7417.03 09270930.78e5d2 76@posting.goog le.com...[color=blue]
    > Hi all,
    > I am writing a project in C++. I am going to compile all my source
    > to a library(.a). So I can only distribute the lib as well as the
    > header file to my client. However, I don't quite familiar with C++
    > programming. In my project, I have defined a heap of classes, the
    > definition of which defined in a hearder file. On the other hand, I
    > would like to limit all the classes in a namespace.
    >
    > Can anyone please tell me how to write the header file? Please give me
    > an simple example.[/color]

    /* =============== =============== = */
    /* myheader.h */
    namespace MyStuff
    {
    class A { /* etc */ };
    class B { /* etc */ };
    }
    /* =============== =============== = */


    If you implement any member functions or static data
    items in a separate '.cpp' file:

    /* =============== =============== = */
    /* myimp.cpp */

    #include "myheader.h "

    namespace MyStuff
    {
    void A::foo() { /* etc */ }
    int B::whatsit = 42;
    }
    /* =============== =============== = */

    Code that uses the classes:

    /* =============== =============== = */
    /* module.cpp */

    #include "myheader.h "

    int main()
    {
    using MyStuff::A;
    using MyStuff::B;

    /* or perhaps simply:
    using namespace MyStuff;
    */
    A thingy;
    thingy.foo();
    /* etc */
    return 0;
    }
    /* =============== =============== = */

    HTH,
    -Mike


    Comment

    • Rex_chaos

      #3
      Re: need help in maintaining a project

      "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:<Wukdb.646 9$NX3.5939@news read3.news.pas. earthlink.net>. ..[color=blue]
      > "Rex_chaos" <rex_chaos@21cn .com> wrote in message
      > news:f7a7417.03 09270930.78e5d2 76@posting.goog le.com...[color=green]
      > > Hi all,
      > > I am writing a project in C++. I am going to compile all my source
      > > to a library(.a). So I can only distribute the lib as well as the
      > > header file to my client. However, I don't quite familiar with C++
      > > programming. In my project, I have defined a heap of classes, the
      > > definition of which defined in a hearder file. On the other hand, I
      > > would like to limit all the classes in a namespace.
      > >
      > > Can anyone please tell me how to write the header file? Please give me
      > > an simple example.[/color]
      >
      > /* =============== =============== = */
      > /* myheader.h */
      > namespace MyStuff
      > {
      > class A { /* etc */ };
      > class B { /* etc */ };
      > }
      > /* =============== =============== = */
      >
      >
      > If you implement any member functions or static data
      > items in a separate '.cpp' file:
      >
      > /* =============== =============== = */
      > /* myimp.cpp */
      >
      > #include "myheader.h "
      >
      > namespace MyStuff
      > {
      > void A::foo() { /* etc */ }
      > int B::whatsit = 42;
      > }
      > /* =============== =============== = */
      >
      > Code that uses the classes:
      >
      > /* =============== =============== = */
      > /* module.cpp */
      >
      > #include "myheader.h "
      >
      > int main()
      > {
      > using MyStuff::A;
      > using MyStuff::B;
      >
      > /* or perhaps simply:
      > using namespace MyStuff;
      > */
      > A thingy;
      > thingy.foo();
      > /* etc */
      > return 0;
      > }
      > /* =============== =============== = */
      >
      > HTH,
      > -Mike[/color]


      Thanks a lot. It do work.

      Comment

      • Rex_chaos

        #4
        Re: need help in maintaining a project

        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:<Wukdb.646 9$NX3.5939@news read3.news.pas. earthlink.net>. ..[color=blue]
        > "Rex_chaos" <rex_chaos@21cn .com> wrote in message
        > news:f7a7417.03 09270930.78e5d2 76@posting.goog le.com...[color=green]
        > > Hi all,
        > > I am writing a project in C++. I am going to compile all my source
        > > to a library(.a). So I can only distribute the lib as well as the
        > > header file to my client. However, I don't quite familiar with C++
        > > programming. In my project, I have defined a heap of classes, the
        > > definition of which defined in a hearder file. On the other hand, I
        > > would like to limit all the classes in a namespace.
        > >
        > > Can anyone please tell me how to write the header file? Please give me
        > > an simple example.[/color]
        >
        > /* =============== =============== = */
        > /* myheader.h */
        > namespace MyStuff
        > {
        > class A { /* etc */ };
        > class B { /* etc */ };
        > }
        > /* =============== =============== = */
        >
        >
        > If you implement any member functions or static data
        > items in a separate '.cpp' file:
        >
        > /* =============== =============== = */
        > /* myimp.cpp */
        >
        > #include "myheader.h "
        >
        > namespace MyStuff
        > {
        > void A::foo() { /* etc */ }
        > int B::whatsit = 42;
        > }
        > /* =============== =============== = */
        >
        > Code that uses the classes:
        >
        > /* =============== =============== = */
        > /* module.cpp */
        >
        > #include "myheader.h "
        >
        > int main()
        > {
        > using MyStuff::A;
        > using MyStuff::B;
        >
        > /* or perhaps simply:
        > using namespace MyStuff;
        > */
        > A thingy;
        > thingy.foo();
        > /* etc */
        > return 0;
        > }
        > /* =============== =============== = */
        >
        > HTH,
        > -Mike[/color]

        Thanks a lot. But I still have a question. Take your example, but in
        my case, class A is a template class

        template<typena me ADT>
        class A
        {...
        class inA
        {
        }
        }

        The more annoying thing is that I have an inner class defined inside
        class A.

        Comment

        • Mike Wahler

          #5
          Re: need help in maintaining a project


          "Rex_chaos" <rex_chaos@21cn .com> wrote in message
          news:f7a7417.03 09280700.5eb2d4 79@posting.goog le.com...[color=blue]
          > "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message[/color]
          news:<Wukdb.646 9$NX3.5939@news read3.news.pas. earthlink.net>. ..[color=blue][color=green]
          > > "Rex_chaos" <rex_chaos@21cn .com> wrote in message
          > > news:f7a7417.03 09270930.78e5d2 76@posting.goog le.com...[color=darkred]
          > > > Hi all,
          > > > I am writing a project in C++. I am going to compile all my source
          > > > to a library(.a). So I can only distribute the lib as well as the
          > > > header file to my client. However, I don't quite familiar with C++
          > > > programming. In my project, I have defined a heap of classes, the
          > > > definition of which defined in a hearder file. On the other hand, I
          > > > would like to limit all the classes in a namespace.
          > > >
          > > > Can anyone please tell me how to write the header file? Please give me
          > > > an simple example.[/color]
          > >
          > > /* =============== =============== = */
          > > /* myheader.h */
          > > namespace MyStuff
          > > {
          > > class A { /* etc */ };
          > > class B { /* etc */ };
          > > }
          > > /* =============== =============== = */
          > >
          > >
          > > If you implement any member functions or static data
          > > items in a separate '.cpp' file:
          > >
          > > /* =============== =============== = */
          > > /* myimp.cpp */
          > >
          > > #include "myheader.h "
          > >
          > > namespace MyStuff
          > > {
          > > void A::foo() { /* etc */ }
          > > int B::whatsit = 42;
          > > }
          > > /* =============== =============== = */
          > >
          > > Code that uses the classes:
          > >
          > > /* =============== =============== = */
          > > /* module.cpp */
          > >
          > > #include "myheader.h "
          > >
          > > int main()
          > > {
          > > using MyStuff::A;
          > > using MyStuff::B;
          > >
          > > /* or perhaps simply:
          > > using namespace MyStuff;
          > > */
          > > A thingy;
          > > thingy.foo();
          > > /* etc */
          > > return 0;
          > > }
          > > /* =============== =============== = */
          > >
          > > HTH,
          > > -Mike[/color]
          >
          > Thanks a lot. But I still have a question. Take your example, but in
          > my case, class A is a template class
          >
          > template<typena me ADT>
          > class A
          > {...
          > class inA
          > {
          > }
          > }[/color]

          };
          [color=blue]
          >
          > The more annoying thing is that I have an inner class defined inside
          > class A.[/color]

          So what's the problem? Do you have code that won't
          compile? That doesn't do what you expect?

          Post the exact code giving you trouble, and we
          can have a look.

          -Mike


          Comment

          • Rex_chaos

            #6
            Re: need help in maintaining a project

            I modify the code as

            /* =============== =============== = */
            /* myheader.h */
            namespace MyStuff
            {
            template <typename ADT>
            class A
            { ...
            A(string str);
            ...
            };

            class B { /* etc */ };
            }

            /* =============== =============== = */
            /* myimp.cpp */

            #include "myheader.h "

            namespace MyStuff
            {
            template <typename ADT>
            A<ADT>::A(strin g str) { /* etc */ }

            / * Code that uses the classes */
            /* =============== =============== = */
            /* module.cpp */

            #include "myheader.h "

            using namespace MyStuff;

            int main()
            {
            A<int> A("hello");

            return 0;
            }

            However, it can't pass the compilation and reports that

            [Linker error] undefined reference to `MyStuff::A<int >::A(std::strin g)'

            Comment

            • Mike Wahler

              #7
              Re: need help in maintaining a project


              "Rex_chaos" <rex_chaos@21cn .com> wrote in message
              news:f7a7417.03 09290048.566657 f7@posting.goog le.com...[color=blue]
              > I modify the code as
              >
              > /* =============== =============== = */
              > /* myheader.h */
              > namespace MyStuff
              > {
              > template <typename ADT>
              > class A
              > { ...[/color]

              Don't put stuff like that in the code you post.
              I have to take it out to compile it.
              [color=blue]
              > A(string str);[/color]

              If you use a standard library type, #include the needed header
              [color=blue]
              > ...[/color]

              And again, I have to edit.
              [color=blue]
              > };
              >
              > class B { /* etc */ };
              > }
              >
              > /* =============== =============== = */
              > /* myimp.cpp */
              >
              > #include "myheader.h "
              >
              > namespace MyStuff
              > {
              > template <typename ADT>
              > A<ADT>::A(strin g str) { /* etc */ }[/color]

              You can't do this. Unless your compiler supports
              'export' (afaik, only Comeau does), you must
              put the full definition of a class template
              in the header.

              [color=blue]
              >
              > / * Code that uses the classes */
              > /* =============== =============== = */
              > /* module.cpp */
              >
              > #include "myheader.h "
              >
              > using namespace MyStuff;
              >
              > int main()
              > {
              > A<int> A("hello");
              >
              > return 0;
              > }
              >
              > However, it can't pass the compilation and reports that
              >
              > [Linker error] undefined reference to `MyStuff::A<int >::A(std::strin g)'[/color]

              That's not a compiler error, it's a linker error.
              Not the same thing.


              /* =============== =============== = */
              /* myheader.h */
              #include <iostream>
              #include <string>

              namespace MyStuff
              {
              template <typename ADT>
              class A
              {
              public:
              // full ctor definition:
              A(const std::string& str)
              {
              std::cout << str << '\n';
              }
              };

              class B { /* etc */ };
              }


              /* =============== =============== = */
              /* myimp.cpp */

              /* nothing left to to */


              /* =============== =============== = */
              /* module.cpp */

              #include "myheader.h "

              using namespace MyStuff;

              int main()
              {
              A<int> A("hello");
              return 0;
              }

              -Mike


              Comment

              Working...