Question

I wrote an Inventory program for groceries in C++ using Visual Studio 2012 and everything runs smoothly and as expected. A file is read in as a command line argument and that is used to fill a deque with Grocery objects. A function that I used to check if a Grocery object was expiring soon used time_t and struct tm. When trying to run my program on Unix, I get an error involving fstream

Here are the lines of code where I am getting the errors:

    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;
}

Errors when trying to run program in 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

Makefile

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}

FIXED Answer listed below

Was it helpful?

Solution

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) constructor is only available in C++11 mode.

You are compiling your code in C++98 mode. Pass -std=gnu++11 to g++ when compiling.

OTHER TIPS

I think you need a clean build. Since you have the declarations in header file, and you might have changed the expDate from type tm to tm*.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top