簡體   English   中英

在gcc中編譯

[英]Compiling in gcc

我有一個正在嘗試在GCC上編譯的程序,盡管出現此錯誤:錯誤:沒有匹配函數調用'std :: basic_ifstream> :: open(std :: string&)'

這是我的代碼:

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

我相信該錯誤與所使用的infile字符串有關,但我聲明了正確的標頭

在C ++ 11之前,您需要將C樣式字符串傳遞給std::ifstream::open 您可以使用std::string::c_str函數從C ++字符串中獲取C樣式字符串,請考慮使用inFile.open(input.c_str()) 但是,如果您使用的是C ++ 11,則無關緊要。

您可能還會收到有關不聲明函數exit的錯誤消息,您可能想在文件頂部添加#include <cstdlib>和其他include,否則請考慮在這種情況下僅使用return 1

您還需要確保使用的是GCC編譯器集合中的C ++編譯器,通常您會使用類似g++ input.cpp -o outputg++ --std=c++11 input.cpp -o output來啟用C ++ 11。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM