簡體   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