繁体   English   中英

运行涉及struct tm的C ++程序的Unix错误

[英]Unix errors running C++ program involving struct tm

我使用Visual Studio 2012为C ++中的杂货编写了一个库存程序,一切运行正常且符合预期。 文件是作为命令行参数读取的,用于用杂货店对象填充双端队列。 我用来检查杂货对象是否即将过期的函数使用了time_t和struct tm。 尝试在Unix上运行程序时, 出现涉及fstream错误

这是出现错误的代码行:

    int main (int argc, char** argv) {

    deque<Grocery> groceries;
    deque<Grocery>::iterator iter;
    string filename = "";

    if (argc > 1) {
        filename = argv[1];
        fstream fileData;
        fileData.open(filename, ios::in | ios::out); //**error**
        //error check
        if (!fileData) {
            cout << "Error openeing file. Program aborting.\n";
            return 1;
        }
        string name;
        string expDate;
        string type;
        string quantity;

        while (!fileData.eof()) {
            Grocery temp;
            getline (fileData,name,'\t');
            temp.setName(name);
            getline (fileData,expDate,'\t');
            temp.setExpDate(expDate);
            getline (fileData,type,'\t');
            temp.setType(type);
            getline (fileData, quantity,'\n');
            temp.setQuantity(atoi (quantity.c_str()));
            groceries.push_back(temp);
        }   
        fileData.close();
    }
    return 0;
}

尝试在Unix中运行程序时出错

  $ make all
g++ -c Grocery.cpp
g++ -c Inventory.cpp
Inventory.cpp: In function âint main(int, char**)â:
Inventory.cpp:24:45: error: no matching function for call to âstd::basic_fstream<char>::open(std::string&, std::_Ios_Openmode)â
   fileData.open(filename, ios::in | ios::out);
                                             ^
Inventory.cpp:24:45: note: candidate is:
In file included from Inventory.cpp:3:0:
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.1/fstream:886:7: note: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       open(const char* __s,
       ^
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.1/fstream:886:7: note:   no known conversion for argument 1 from âstd::string {aka std::basic_string<char>}â to âconst char*â
make: *** [Inventory.o] Error 1

生成文件

MKFILE    = Makefile

#
# Definitions of list of files:
#
HSOURCES  = Grocery.h
CSOURCES  = Grocery.cpp Inventory.cpp

ETCSRC    = README ${MKFILE}
EXECBIN   = main
ALLCSRC   = ${CSOURCES}
OBJECTS   = ${ALLCSRC:.cpp=.o}
ALLSRC    = ${ETCSRC} ${HSOURCES} ${CSOURCES}
LISTSRC   = ${ALLSRC}

#
# Definitions of the compiler and compilation options:
#
GCC       = g++

#
# The first target is always ``all'', and hence the default,
# and builds the executable images
#
all : ${EXECBIN}

#
# Build the executable image from the object files.
#
${EXECBIN} : ${OBJECTS}
        ${GCC} -o ${EXECBIN} ${OBJECTS}

#
# Build an object file form a C source file.
#
%.o : %.cpp
        ${GCC} -c $<

#
# Clean and spotless remove generated files.
#
clean :
        - rm -rf ${EXECBIN} ${OBJECTS}

下面列出了固定答案

Inventory.cpp:24:45: error: no matching function for call to âstd::basic_fstream<char>::open(std::string&, std::_Ios_Openmode)â
fileData.open(filename, ios::in | ios::out);

basic_fstream(const string&, ios_base::openmode)构造函数仅在C ++ 11模式下可用。

您正在C ++ 98模式下编译代码。 编译时将-std=gnu++11传递给g++

我认为您需要一个干净的构建。 由于您在头文件中有声明,因此可能已将expDate从类型tm更改为tm *。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM