namespace question

Discussion in 'C++' started by John, Jun 2, 2004.

  1. John

    John Guest

    Hi all:

    I am using a code from third party. I write my own part and add it to
    that code and compile together.
    I try to use "vector" in my program. So I add "#include <vector>" to
    my program.
    But when I compile my program together with the code from the third
    party,
    the compiler reports error: redefinition of `class vector'.

    I look at the code from the third party and find that in one file of
    that code,
    "class vector" is defined for other purpose.
    I can not modify the code from the third party, because it may make
    the situation worse.
    What should I do to my own program to solve the name confliction
    problem?

    Thanks a lot.

    John
     
    John, Jun 2, 2004
    #1
    1. Advertising

  2. John

    Sharad Kala Guest

    "John" <> wrote in message
    news:...
    > Hi all:
    >
    > I am using a code from third party. I write my own part and add it to
    > that code and compile together.
    > I try to use "vector" in my program. So I add "#include <vector>" to
    > my program.
    > But when I compile my program together with the code from the third
    > party,
    > the compiler reports error: redefinition of `class vector'.
    >
    > I look at the code from the third party and find that in one file of
    > that code,
    > "class vector" is defined for other purpose.
    > I can not modify the code from the third party, because it may make
    > the situation worse.
    > What should I do to my own program to solve the name confliction
    > problem?


    Don't write using namesapce std; in your code. You will need to refer to the
    standard vector class as std::vector then.
     
    Sharad Kala, Jun 2, 2004
    #2
    1. Advertising

  3. John

    Rolf Magnus Guest

    John wrote:

    > Hi all:
    >
    > I am using a code from third party. I write my own part and add it to
    > that code and compile together.
    > I try to use "vector" in my program. So I add "#include <vector>" to
    > my program.
    > But when I compile my program together with the code from the third
    > party, the compiler reports error: redefinition of `class vector'.
    >
    > I look at the code from the third party and find that in one file of
    > that code, "class vector" is defined for other purpose.
    > I can not modify the code from the third party, because it may make
    > the situation worse.
    > What should I do to my own program to solve the name confliction
    > problem?


    The C++ standard class vector is in namespace std, so unless your 3rd
    party library puts it there, too, there shouldn't be a name conflict.
    After all, that's why namespaces exist.
    If your compiler doesn't put vector into namespace std, then it is
    probably quite outdated and you should consider using a more recent
    one.
     
    Rolf Magnus, Jun 2, 2004
    #3
  4. John

    JKop Guest

    John posted:

    > Hi all:
    >
    > I am using a code from third party. I write my own part and add it to
    > that code and compile together.
    > I try to use "vector" in my program. So I add "#include <vector>" to
    > my program.
    > But when I compile my program together with the code from the third
    > party,
    > the compiler reports error: redefinition of `class vector'.
    >
    > I look at the code from the third party and find that in one file of
    > that code,
    > "class vector" is defined for other purpose.
    > I can not modify the code from the third party, because it may make
    > the situation worse.
    > What should I do to my own program to solve the name confliction
    > problem?
    >
    > Thanks a lot.
    >
    > John



    namespace ThirdParty
    {

    #include <thirdpary>
    }


    std::vector
    ThirdParty::vector


    using namespace std


    vector //refers to std::vector



    -JKop
     
    JKop, Jun 2, 2004
    #4
  5. John

    Dave Moore Guest

    (John) wrote in message news:<>...
    > Hi all:
    >
    > I am using a code from third party. I write my own part and add it to
    > that code and compile together.
    > I try to use "vector" in my program. So I add "#include <vector>" to
    > my program.
    > But when I compile my program together with the code from the third
    > party,
    > the compiler reports error: redefinition of `class vector'.
    >
    > I look at the code from the third party and find that in one file of
    > that code,
    > "class vector" is defined for other purpose.
    > I can not modify the code from the third party, because it may make
    > the situation worse.
    > What should I do to my own program to solve the name confliction
    > problem?


    Well, the vector template that is defined in <vector> resides in
    namespace std, so you should still be able to use it even in the
    presence of the other vector class by specifying std::vector instead
    of just vector. Note also that getting rid of using directives (e.g.
    using namespace std) is generally a good idea, and will help identify
    more name collisions between std::vector and your vendors' vector as
    well.

    HTH, Dave Moore



    > Thanks a lot.
    >
    > John
     
    Dave Moore, Jun 2, 2004
    #5
  6. John

    David Harmon Guest

    On Wed, 02 Jun 2004 12:32:50 GMT in comp.lang.c++, JKop <>
    wrote,
    >namespace ThirdParty
    >{
    >
    > #include <thirdpary>
    >}


    You think so, huh? Where is the linker going to find the definition of
    ThirdParty::vector or anything else?
     
    David Harmon, Jun 2, 2004
    #6
  7. John

    John Guest

    Hi all:

    Thanks for reply.
    In my program, I already use std::vector except the line: #include
    <vector>.
    Below is part of the error message:
    In file included from
    /usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/vector.h:32,
    from mycode.cc:35:
    /usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/stl_vector.h:153:
    `vector' is not a template type
    /usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/stl_vector.h:154:
    redefinition of `class vector'
    mobile/god.h:117: previous definition here
    /usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/stl_vector.h:156:
    invalid member template declaration `vector::_Base'
    /usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/stl_vector.h:158:
    invalid member template declaration `vector::value_type'

    mycode.cc is my own code. Line 35 is #include <vector>. mobile/god.h
    is the code
    from third party.
    I also use list in my code, the compiler does not complain it.

    Thanks a lot.

    John

    Rolf Magnus <> wrote in message news:<c9k72p$3q5$01$-online.com>...
    > John wrote:
    >
    > > Hi all:
    > >
    > > I am using a code from third party. I write my own part and add it to
    > > that code and compile together.
    > > I try to use "vector" in my program. So I add "#include <vector>" to
    > > my program.
    > > But when I compile my program together with the code from the third
    > > party, the compiler reports error: redefinition of `class vector'.
    > >
    > > I look at the code from the third party and find that in one file of
    > > that code, "class vector" is defined for other purpose.
    > > I can not modify the code from the third party, because it may make
    > > the situation worse.
    > > What should I do to my own program to solve the name confliction
    > > problem?

    >
    > The C++ standard class vector is in namespace std, so unless your 3rd
    > party library puts it there, too, there shouldn't be a name conflict.
    > After all, that's why namespaces exist.
    > If your compiler doesn't put vector into namespace std, then it is
    > probably quite outdated and you should consider using a more recent
    > one.
     
    John, Jun 2, 2004
    #7
  8. John

    Rolf Magnus Guest

    John wrote:

    > Hi all:
    >
    > Thanks for reply.
    > In my program, I already use std::vector except the line: #include
    > <vector>.
    > Below is part of the error message:
    > In file included from
    > /usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/vector.h:32,

    ^^^^^^
    This is your problem. The compiler is too old. g++ versions before 3.x
    are not compliant wrt namespace std. They make std a synonym for the
    global namespace. If possible, you should upgrade to gcc 3.x.
     
    Rolf Magnus, Jun 2, 2004
    #8
  9. John

    JKop Guest

    David Harmon posted:

    > On Wed, 02 Jun 2004 12:32:50 GMT in comp.lang.c++, JKop <>
    > wrote,
    >>namespace ThirdParty {
    >>
    >> #include <thirdpary> }

    >
    > You think so, huh? Where is the linker going to find the definition of
    > ThirdParty::vector or anything else?



    Let's assume that there's:


    ThirdParty.hpp
    ThirdParty.cpp



    In the Source Code file in which you wish to use this library, put:

    namespace ThirdParty
    {
    #include <thirdparty.hpp>
    }



    And now, for the Source code file... Don't actually add it to your project
    to be compiled; instead, do this: Make another file
    "ThirdPartySourceCode.cpp", and put the following into it:


    namespeace ThirdParty
    {

    using namespace ThirdParty;

    #include <thirdparty.cpp>
    }



    -JKop
     
    JKop, Jun 2, 2004
    #9
  10. John

    David Harmon Guest

    On Wed, 02 Jun 2004 18:16:05 GMT in comp.lang.c++, JKop <>
    wrote,
    >Let's assume that there's:
    >
    >
    >ThirdParty.hpp
    >ThirdParty.cpp


    No, that's too easy. Instead there is

    ThirdParty.hpp
    ThirdParty.lib
     
    David Harmon, Jun 2, 2004
    #10
  11. John

    JKop Guest

    David Harmon posted:

    > On Wed, 02 Jun 2004 18:16:05 GMT in comp.lang.c++, JKop <>
    > wrote,
    >>Let's assume that there's:
    >>
    >>
    >>ThirdParty.hpp
    >>ThirdParty.cpp

    >
    > No, that's too easy. Instead there is
    >
    > ThirdParty.hpp
    > ThirdParty.lib
    >



    Library files are the devil!


    -JKop
     
    JKop, Jun 3, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    592
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,156
  3. Anonymous
    Replies:
    3
    Views:
    554
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    462
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,418
    Howard
    Nov 29, 2004
Loading...

Share This Page