2 compile problems

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

    2 compile problems

    I have the following 2 files as part of my project:

    GenFuncs.h
    ------------
    #ifndef GenFuncsH
    #define GenFuncsH
    #include <iostream.h>
    #include <strstream.h>
    using namespace std;

    //template <class T> string str(T In);
    string Int2Hex(int In);

    template <class T> string str(T In) {
    ostrstream oss;
    oss << In;
    return(oss.str( ));
    }

    #endif

    GenFuncs.cpp
    ------------
    #pragma hdrstop
    #include "GenFuncs.h "
    #pragma package(smart_i nit)
    string Int2Hex(int In) {
    ostrstream oss;
    oss << hex << In;
    return(oss.str( ));
    }


    Now, if I compile this code "as is" in Borland, it compiles OK, and I am
    able to use both the Int2Hex() and str() functions in my other code that
    #includes GenFuncs.h.

    Problem #1

    when I try to compile in gnu with:

    g++ -g -c GenFuncs.cpp

    I get

    GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex

    It seems that it does not recognize the string identifier (which I don't
    understand, because I am using namespace std)

    Problem #2

    Ideally, I would like to have the declaration for the str() function in the
    GenFuncs.h file and the body in the GenFuncs.cpp file. However, if I do
    this (uncomment the declaration and move the body to .cpp), then I get
    linker errors in Borland like thus:

    [Linker Error] Unresolved external 'std::basic_str ing<char,
    std::char_trait s<char>, std::allocator< char> > str<double>(dou ble)'
    referenced from C:\PROGRAMS\PRO J\TESTCLASS.OBJ .

    I get a linker error for each function call to the template function. Note
    that if I replace the template declaration and body with overloaded
    non-template versions, then it compiles fine.

    Any help is appreciated...

    Vic





  • Gianni Mariani

    #2
    Re: 2 compile problems

    Victor Hannak wrote:[color=blue]
    > I have the following 2 files as part of my project:
    >
    > GenFuncs.h
    > ------------
    > #ifndef GenFuncsH
    > #define GenFuncsH
    > #include <iostream.h>[/color]

    #include <iostream> // iostream.h is deprecated - don't used them
    [color=blue]
    > #include <strstream.h>
    > using namespace std;
    >
    > //template <class T> string str(T In);
    > string Int2Hex(int In);
    >
    > template <class T> string str(T In) {
    > ostrstream oss;
    > oss << In;
    > return(oss.str( ));
    > }
    >
    > #endif
    >
    > GenFuncs.cpp
    > ------------
    > #pragma hdrstop
    > #include "GenFuncs.h "
    > #pragma package(smart_i nit)
    > string Int2Hex(int In) {
    > ostrstream oss;
    > oss << hex << In;
    > return(oss.str( ));
    > }
    >
    >
    > Now, if I compile this code "as is" in Borland, it compiles OK, and I am
    > able to use both the Int2Hex() and str() functions in my other code that
    > #includes GenFuncs.h.
    >
    > Problem #1
    >
    > when I try to compile in gnu with:
    >
    > g++ -g -c GenFuncs.cpp
    >
    > I get
    >
    > GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex
    >
    > It seems that it does not recognize the string identifier (which I don't
    > understand, because I am using namespace std)
    >
    > Problem #2
    >
    > Ideally, I would like to have the declaration for the str() function in the
    > GenFuncs.h file and the body in the GenFuncs.cpp file. However, if I do
    > this (uncomment the declaration and move the body to .cpp), then I get
    > linker errors in Borland like thus:
    >
    > [Linker Error] Unresolved external 'std::basic_str ing<char,
    > std::char_trait s<char>, std::allocator< char> > str<double>(dou ble)'
    > referenced from C:\PROGRAMS\PRO J\TESTCLASS.OBJ .
    >
    > I get a linker error for each function call to the template function. Note
    > that if I replace the template declaration and body with overloaded
    > non-template versions, then it compiles fine.
    >
    > Any help is appreciated...
    >[/color]

    Try this. It may solve problem 2, if not then I suggest you contact a
    borland NG.

    ::::::::::::::
    GenFuncs.h
    ::::::::::::::
    #ifndef GenFuncsH
    #define GenFuncsH
    #include <iostream>
    #include <sstream>
    using namespace std;

    //template <class T> string str(T In);
    string Int2Hex(int In);

    template <class T> string str(T In) {
    ostringstream oss;
    oss << In;
    return(oss.str( ));
    }

    #endif

    ::::::::::::::
    GenFuncs.cpp
    ::::::::::::::
    #pragma hdrstop
    #include "GenFuncs.h "
    #pragma package(smart_i nit)
    string Int2Hex(int In) {
    ostringstream oss;
    oss << hex << In;
    return(oss.str( ));
    }

    Comment

    • Artie Gold

      #3
      Re: 2 compile problems

      Victor Hannak wrote:[color=blue]
      > I have the following 2 files as part of my project:
      >
      > GenFuncs.h
      > ------------
      > #ifndef GenFuncsH
      > #define GenFuncsH
      > #include <iostream.h>[/color]

      No such header in standard C++ (iostream.h is a holdover from
      pre-standard days).

      #include <iostream>
      [color=blue]
      > #include <strstream.h>[/color]

      Similarly.

      #include <sstream>

      For the error below:

      #include <string>
      [color=blue]
      > using namespace std;[/color]

      Putting a using directive like this in a header is a Bad Idea, as it
      pollutes the namespace in a way that might not be immediately
      apparent. It would be better to use fully qualified names.
      [color=blue]
      >
      > //template <class T> string str(T In);
      > string Int2Hex(int In);[/color]

      You didn't include <string>. See above.
      [color=blue]
      >
      > template <class T> string str(T In) {
      > ostrstream oss;
      > oss << In;
      > return(oss.str( ));
      > }
      >
      > #endif
      >
      > GenFuncs.cpp
      > ------------
      > #pragma hdrstop[/color]

      Non standard. Please remove non standard things from code you post
      to news:comp.lang. c++.
      [color=blue]
      > #include "GenFuncs.h "
      > #pragma package(smart_i nit)
      > string Int2Hex(int In) {
      > ostrstream oss;
      > oss << hex << In;
      > return(oss.str( ));
      > }
      >
      >
      > Now, if I compile this code "as is" in Borland, it compiles OK, and I am
      > able to use both the Int2Hex() and str() functions in my other code that
      > #includes GenFuncs.h.
      >
      > Problem #1
      >
      > when I try to compile in gnu with:
      >
      > g++ -g -c GenFuncs.cpp
      >
      > I get
      >
      > GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex
      >
      > It seems that it does not recognize the string identifier (which I don't
      > understand, because I am using namespace std)[/color]

      But you didn't `#include <string>"
      [color=blue]
      >
      > Problem #2
      >
      > Ideally, I would like to have the declaration for the str() function in the
      > GenFuncs.h file and the body in the GenFuncs.cpp file. However, if I do
      > this (uncomment the declaration and move the body to .cpp), then I get
      > linker errors in Borland like thus:
      >
      > [Linker Error] Unresolved external 'std::basic_str ing<char,
      > std::char_trait s<char>, std::allocator< char> > str<double>(dou ble)'
      > referenced from C:\PROGRAMS\PRO J\TESTCLASS.OBJ .
      >
      > I get a linker error for each function call to the template function. Note
      > that if I replace the template declaration and body with overloaded
      > non-template versions, then it compiles fine.[/color]

      Since your compilers do not support the `export' keyword (few do,
      only Comeau comes to mind, though I think there may be others), all
      templated code must be visible in any translation unit in which it
      is used.

      HTH,
      --ag


      --
      Artie Gold -- Austin, Texas

      Comment

      • Kevin Goodsell

        #4
        Re: 2 compile problems

        Victor Hannak wrote:
        [color=blue]
        > I have the following 2 files as part of my project:
        >
        > GenFuncs.h
        > ------------
        > #ifndef GenFuncsH
        > #define GenFuncsH
        > #include <iostream.h>[/color]

        Non-standard header. Use <iostream> instead.
        [color=blue]
        > #include <strstream.h>[/color]

        Non-standard header. Use <sstream>.
        [color=blue]
        > using namespace std;[/color]

        Bad idea in a header file. The person #including your header may not
        want the entire std namespace included.
        [color=blue]
        >
        > //template <class T> string str(T In);
        > string Int2Hex(int In);[/color]

        What is 'string'? If you mean std::string, you'd better include the
        appropriate header (<string>).
        [color=blue]
        >
        > template <class T> string str(T In) {
        > ostrstream oss;[/color]

        Use std::ostringstr eam instead.
        [color=blue]
        > oss << In;
        > return(oss.str( ));
        > }
        >
        > #endif
        >
        > GenFuncs.cpp
        > ------------
        > #pragma hdrstop[/color]

        This is non-standard.
        [color=blue]
        > #include "GenFuncs.h "
        > #pragma package(smart_i nit)[/color]

        This is non-standard.
        [color=blue]
        > string Int2Hex(int In) {
        > ostrstream oss;
        > oss << hex << In;
        > return(oss.str( ));
        > }
        >
        >
        > Now, if I compile this code "as is" in Borland, it compiles OK,[/color]

        That would be luck.
        [color=blue]
        > and I am
        > able to use both the Int2Hex() and str() functions in my other code that
        > #includes GenFuncs.h.
        >
        > Problem #1
        >
        > when I try to compile in gnu with:
        >
        > g++ -g -c GenFuncs.cpp
        >
        > I get
        >
        > GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex
        >
        > It seems that it does not recognize the string identifier (which I don't
        > understand, because I am using namespace std)[/color]

        That isn't the issue. 'using namespace std' does not tell the compiler
        what 'string' is. You do that by #including <string>.
        [color=blue]
        >
        > Problem #2
        >
        > Ideally, I would like to have the declaration for the str() function in the
        > GenFuncs.h file and the body in the GenFuncs.cpp file.[/color]

        Read a little bit about templates. Unless your compiler supports
        'export' (I only know of 1 that does), you are out of luck.



        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Victor Hannak

          #5
          Re: 2 compile problems

          Thanks for the great suggestions, they helped me get over those hurdles.

          However, there is something that is unclear to me in all of this...

          You all told me to use the <sstream> header in conjunction with the
          ostringstream type. This worked fine in Borland, but gnu choked. However,
          when I switched back to <strstream> and ostrstream, both compilers accepted
          it just fine. Of course my C++ textbook (C++ How to Program by
          Deitel/Deitel) still uses the <strstream.h> notation. Can someone just
          comment on the difference between <strstream> and <sstream>.

          After looking at bok reviews on www.accu.org, I am thinking that "The C++
          Programming Language Special Edition", by Bjarne Stroustrup seems like my
          best bet for an updated C++ general reference book. Does this sound right?



          Comment

          • Peter van Merkerk

            #6
            Re: 2 compile problems

            > After looking at bok reviews on www.accu.org, I am thinking that "The
            C++[color=blue]
            > Programming Language Special Edition", by Bjarne Stroustrup seems like[/color]
            my[color=blue]
            > best bet for an updated C++ general reference book. Does this sound[/color]
            right?

            Yes.

            --
            Peter van Merkerk
            peter.van.merke rk(at)dse.nl


            Comment

            • Julián Albo

              #7
              Re: 2 compile problems

              Victor Hannak escribió:
              [color=blue]
              > You all told me to use the <sstream> header in conjunction with the
              > ostringstream type. This worked fine in Borland, but gnu choked. However,
              > when I switched back to <strstream> and ostrstream, both compilers accepted[/color]

              If you have an old version of gcc you can download a sstream file from
              gcc.gnu.org (the address is in the faq of the site, I think), is not a
              complete standard implementation but works in many uses.

              Regards.

              Comment

              • Pete Becker

                #8
                Re: 2 compile problems

                Gianni Mariani wrote:[color=blue]
                >
                > #include <iostream> // iostream.h is deprecated - don't used them
                >[/color]

                Actually, it's not deprecated. It's ignored. <g> In the standard,
                "deprecated " means that something is currently part of the language, but
                you're warned that it may go away in a subsequent version of the
                standard. Since iostream.h was never part of the C++ standard, there's
                nothing to deprecate.

                --

                Pete Becker
                Dinkumware, Ltd. (http://www.dinkumware.com)

                Comment

                Working...