我得到不能用作功能错误?

时间:2016-02-24 15:42:35

标签: c++

我没有得到我想要的结果,帮助我解决这个问题。错误是什么? 应该在头文件中使用哪些函数? 什么功能不应该? 我的主文件 - >

#include <iostream> 
#include <string>
#include "flight.h"
using namespace std;

int main ()
{
int rows=0,cols=0;
int ** readDurations= NULL;
int * cities = NULL;
string duration = "durations.txt";
readDurations(duration, rows, cols);
}

我的flight.cpp文件 - &gt;

#ifndef FLIGHT_H    
#define FLIGHT_H

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

// inputs the file fileName, creates a dynamic 2D array, and returns it
int** readDurations(string filename, int& rows, int& cols)
{
    ifstream myfile("durations.txt");
    if (myfile)
    cout << "Error in opening file!";
    myfile >> rows;
    cols=rows;

    int **readDurations = new int *[rows];
    for (int i=0;i<rows;i++){
        readDurations[i] = new int [cols];
    }


    for (int i=0;i<rows;i++){
        for (int j=0;j<cols;j++){
            myfile >> readDurations[i][j];
        }
    }

    return readDurations;

}

我的头文件

#ifndef FLIGHT_H
#define FLIGHT_H

#include <string>
using namespace std;

// inputs the file fileName, creates a dynamic 2D array, and returns it
int** readDurations(string fileName, int& rows, int& cols);
#endif

1 个答案:

答案 0 :(得分:4)

main()中的

int ** readDurations= NULL;
// ...
readDurations(duration, rows, cols); // it thinks this is an int**

您对两个不同的东西使用相同的名称。 将您的函数放在命名空间中,然后将其称为完全限定:myNameSpace::readDurations(...);

但是你还没有使用此处声明的readDurations变量。摆脱它。