编译错误在c++中使用Linux程序制作

Compile error in C++ using Linux program make

本文关键字:Linux 程序 错误 c++ 编译      更新时间:2023-10-16

我目前正在尝试将这部分代码从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++中这是不必要的。我不明白为什么它告诉我iostreamstrstream超出了范围…

您使用了已弃用(且拼写错误)的标头。

使用<sstream><iostream>代替

你有打字错误:

oststream //^^typo

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

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

使用正确的命名空间std:

std::cout;
std::endl;