STL string wrapper

This is a discussion on STL string wrapper within the C++ Programming forums, part of the General Programming Boards category; I am trying to wrap an old library using STL so we don't have to change all the 50 applications ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    STL string wrapper

    I am trying to wrap an old library using STL so we don't have to change all the 50 applications to use STL. The old library used the IString class so here is what my test wrapper class looks like but the problem is that when I try to compile the applications with this class I get a bezillion errors. I am guessing it has got something to do with the old style C++ vs new. All the old apps use the extern'C' linkage for the header files and this new stuff is using #include <string> and 'using namespace std' so that is where the conflict lies. Here is my test class:


    IString.hpp
    ----------------------
    Code:
    #include <string>
    using namespace std;
    
    class IString
    {
    	private:
    		string buffer;
    		
    	public:
    		IString (char* str);  
    		IString (string str);
    		unsigned length( ); 
    		IString subString(unsigned startPos, unsigned length);
                    
    };
    IString.cpp
    --------------------------
    Code:
    #include <istring.hpp>
    
    IString::IString(char* str)
    {
    	buffer = str;	
    }
    
    IString::IString(string str)
    {
    	buffer = str;	
    }
    
    IString IString::subString(unsigned startPos, unsigned length)
    {
    	return IString(buffer.substr(startPos, length));
    }
    
    
    unsigned IString::length()
    {
        return buffer.length();
    }
    and this is the error I get when I comment out 'using namespace std':

    /usr/vacpp/bin/xlC_r -c -qsource -qlistopt -qxref -qattr -I/usr/lpp/db2_07_01/include -I/usr/vacpp/include -I/usr/vac/include -I/h/IOCDir/IDate -I/h/IOCDir/ITime -I/h/IOCDir/IString brocDB2.C
    "/h/IOCDir/IString/istring.hpp", line 24.24: 1540-0040 (S) The text "buffer" is unexpected. "string" may be undeclared or ambiguous.
    "/h/IOCDir/IString/istring.hpp", line 21.7: 1540-0403 (S) "class IString" is already defined.
    "/h/IOCDir/IString/istring.hpp", line 21.7: 1540-0403 (S) "class IString" is already defined.
    "brocDB2.sqC", line 50.6: 1540-1600 (W) "main" should have a return type of type "int".
    make: 1254-004 The error code from the last command is 1.



    and when I leave it in there I get TONS of errors.

    Can someone shed some light on this subject?

    Thanks!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    One solution is to compile the class into an object file. In the header, just add a void* that is actually an std::string*, ie:


    Code:
    // IString.hpp
    
    class IString
    {
    	private:
    		void * buffer;
    		
    	public:
    		~IString ();
                                    IString (char* str);  
    		IString (string str);
    		unsigned length( ); 
    		IString subString(unsigned startPos, unsigned length);
                    
    };

    Then, for the object file:


    Code:
    // IString.cpp
    
    #include <IString.hpp>
    #include <string>
    
    using namespace std;
    
    IString::~IString()
    {
     delete (string*)buffer;
    }
    
    IString::IString(char * s)
    {
     buffer = new string;
     *((string*)buffer) = s;
    }
    
    //...etc...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    Thanks for the idea but what do I do with the string being used in the constructor in the header file:

    IString::IString(string s)

    because this is going to require '#include<string>' in the header file.

    Am I missing something?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ah, I overlooked that. Anyway, since the header file is going to be used by code that doesn't directly use an std::string, just omit it that constructor - right?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    This compiled just fine for me. I left the namespace std in. What errors do you get when you take it out?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21