-1

我目前正在尝试将这部分代码从 C 移植到 C++。它基本上将 snprintf 语句替换为 C++ 实现。

   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
        oststream 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);

我已经把原文注释掉了,snprintf所以我有它的参考。我需要使用cout、 OSS 和oststream. 我已经包含了正确的标题iostreamstrsteam和“使用命名空间 std;”。但是,当我使用“make”运行它时,出现以下错误

g++ -g -Wno-deprecated -c fmttime.cc fmttime.o
fmttime.cc:14:22: error: strsteam.h: No such file or directory
fmttime.cc:15:21: error: iosteam.h: No such file or directory
fmttime.cc: In function 'char* formatTime(timeval*, char*, size_t)':
fmttime.cc:273: error: 'oststream' was not declared in this scope
fmttime.cc:273: error: expected ';' before 'oss'
fmttime.cc:275: error: 'oss' was not declared in this scope
fmttime.cc:275: error: 'ends' was not declared in this scope
fmttime.cc:276: error: 'cout' was not declared in this scope
fmttime.cc:276: error: 'endl' was not declared in this scope
make: *** [fmttime.o] Error 1

这是我的制作文档供参考。

#Variables
PROGRAM = plot
OBJS = plot.o fmttime.o
CXXFLAGS = -g -Wno-deprecated
LIBS = -lm -lcurses

#Rules
all : ${PROGRAM}

${PROGRAM} : ${OBJS}
        g++ ${OBJS} ${LIBS} -o ${PROGRAM}

%.o : %.cc
        g++ ${CXXFLAGS} -c $< $@

clean:
        rm ${OBJS} ${PROGRAM}

所以我一直在尝试一切,我在标题之后包含了 .h,但我知道在 C++ 中它不是必需的。我不明白为什么它告诉我iostream并且strstream超出范围......

4

2 回答 2

2

您正在使用已弃用(和拼写错误)的标题。

使用<sstream>and<iostream>代替。

于 2013-04-14T20:01:04.777 回答
1

你有错字:

oststream //^^typo

您需要包含正确的头文件:

 #include <iostream>
 #include <sstream> //not strstream, deprecated

使用正确的命名空间标准:

std::cout;
std::endl;

或使用命名空间标准;

于 2013-04-14T20:01:27.930 回答