我目前正在尝试将这部分代码从 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
. 我已经包含了正确的标题iostream
、strsteam
和“使用命名空间 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
超出范围......