Jsosa6
Jsosa6

Reputation: 31

Compiling in gcc

I have a program i am trying to compile on GCC, though this error appears: error: no matching function for call to ‘std::basic_ifstream >::open(std::string&)’

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;
int main() {

    int column, row, total, counter;
    column = 1;
    row = 1;


    cout << "x-dimensions of array: ";
    cin >> row;
    cout << "y-dimensions of array: ";
    cin >> column;
    total = row*column;
    double myArray[row][column];
    double *myPtr;
    myPtr = *myArray;



    string input;
    cout << "Enter text file name: ";
    cin >> input;

    ifstream inFile;
    inFile.open(input);

    //Check for Error
    if (inFile.fail()){
        cerr << "Error opening file" << endl;
        exit(1);
    }

    for (int i = 0; i < total; i++){

        inFile >> *(myPtr+i);
}

i believe the error has to do with the infile string being used, but i declared the proper headers

Upvotes: 0

Views: 62

Answers (1)

ilent2
ilent2

Reputation: 5241

Prior to C++11 you needed to pass a C style string to std::ifstream::open. You can get a C style string from a C++ string using the std::string::c_str function, consider using inFile.open(input.c_str()). However, if you are using C++11 this shouldn't matter.

You may also be getting an error about not declaring the function exit, you might want to add #include <cstdlib> with the other includes at the top of your file, otherwise consider just using return 1 in this instance.

You also need to make sure you are using the C++ compiler from the GCC compiler collection, typically you would use something like g++ input.cpp -o output or g++ --std=c++11 input.cpp -o output to enable C++11.

Upvotes: 2

Related Questions