Question

I was wondering if i could get some help. I'm trying to port over some code as required in a lab assignment given to us. Basically I've created some program in C, and part of the requirement now is to port over the printf and snprintf statements into C++. printf I think I can manage, as it seems to be standard using cout << "whatever"<< endl. The problem is with snprintf, and replacing it using ostream oss(). Here is a partition of the code I am trying to resolve.

char* formatTime(
                struct timeval* tv,     // Pointer to timeval struct
                char* buf,              // Pointer to char buf
                size_t len              // size of buffer
                )
{
    struct ExpandedTime etime2;         // Struct object declaration


    // Array containing strings for the months

    const char* month[] =
        {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
        "Aug", "Sep", "Oct", "Nov", "Dec"
        };


    if (len > 0)                        // If statement for 0 length buf error
    {
        localTime(tv, &etime2);

        // Printing to the supplied buffer in the main program
        char timebuf[80];               // Buffer to hold the time stamp
        ostream oss(timebuf, sizeof(timebuf));

        oss << etime2.et_year << ends;
        cout << timebuf << endl;
//      snprintf(buf, len, "%02i %s %02i %02i:%02i:%02i:%03i",
//      etime2.et_year, month[etime2.et_mon], etime2.et_day, etime2.et_hour,
//      etime2.et_min, etime2.et_sec, etime2.et_usec);
    }

Now i've commented out the original snprintf statement. And im just testing the "year" parameter which is store in some structure. However I keep getting the following errors.

rkim@l3055serv:~/plot$ make
g++ -g -Wno-deprecated -c fmttime.cc fmttime.o
fmttime.cc: In function 'char* formatTime(timeval*, char*, size_t)':
fmttime.cc:273: error: no matching function for call to 'std::basic_ostream<char, std::char_traits<char> >::basic_ostream(char [80], unsigned int)'
/usr/include/c++/4.4/ostream:361: note: candidates are: std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:82: note:                 std::basic_ostream<_CharT, _Traits>::basic_ostream(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:56: note:                 std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)
make: *** [fmttime.o] Error 1

I've made sure to include iostream and sstream as well as using namespace std; however I still get this error and am not able to resolve it. To be honest, we have just started learning C++ so part of the problem is the whole abstractness of C++. However this partition of code using osream and oss was given to us by our lab instructor and besides him using the wrong headers "strstream and oststream" to my knowledge of C++ thus far it seems to make sense....

Was it helpful?

Solution

You're looking for std::ostringstream, not ostream.

OTHER TIPS

I suggest you replace ostream with ostringstream.

The ostream is an abstract, generic class.

ostream doesn't have such a constructor, you probably wanted to use an ostringstream and something like:

std::ostringstream oss;
oss << etime2.et_year << ends;

std::cout << oss.str() << endl;

... but then there's not much point in using oss as you may as well stream straight to std::cout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top