尝试将程序提交给教师评分网站

时间:2016-12-12 02:31:11

标签: c++ compilation segmentation-fault

嗨,所以我有一个项目,我正在尝试将其上传到一个自动评分我的项目的网站上。唯一的问题是,由于某种原因,当我将文件提交到网站时,我的文件没有编译。我尝试在我的计算机上编译并且没有错误弹出,即使我运行可执行文件它也能完美运行。我可能看到的唯一问题是,当我尝试双击可执行文件并输入相同的信息时,我最终得到了一个

'Segmentation fault: 11'

有人请帮助我了解这个错误的来源。

(只是为了澄清,这个分段错误只发生在我双击可执行文件时,但是当我从控制台运行它时,它会编译并运行而没有错误)

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <math.h> //sqrt function

using namespace std;

struct Planet{
    int row;
    int col;
    string name;
    string symbol;
    int id;
};

vector< vector<string> > makeGrid(int rows, int cols);
void fillVector(vector< vector<string> > &grid);
void printVectorGrid(vector< vector<string> > grid);
void printVector(vector<Planet> p);
void printPlanet(Planet p);

double distance(Planet &a, Planet &b){
    int colDif = a.col-b.col;
    int rowDif = a.row-b.row;
    return sqrt(rowDif*rowDif*1.0 + colDif*colDif);
}
vector<Planet> getShortestPath(int srow, int scol, int erow, int ecol ,vector<Planet> v){
    Planet temp;
    temp.row = srow;
    temp.col = scol;
    vector<Planet> thing;
    while(v.size()>0){
        double minDist = 100000000;
        int index = 0;
        for(int i=v.size()-1; i>=0; i--){
            if(minDist >= distance(temp,v.at(i))){
                index = i;
                minDist = distance(temp, v.at(i));
            }
        }

        temp = v.at(index);
        thing.push_back(temp);      
        v.erase(v.begin()+index);

    }
    return thing;
}


int main(){
    int rows, cols, startRow, startCol, endRow, endCol;
    string nameFile, locationFile;
    cout << "Enter Locations Filename: ";
    cin >> locationFile;
    cout << "Enter Names Filename: ";
    cin >> nameFile;


    ifstream nameIn(nameFile.c_str());
    ifstream locationIn(locationFile.c_str());

    locationIn >> rows >> cols >> startRow >> startCol >> endRow >> endCol;

    int tempRow, tempCol, tempId;
    string tempSymbol;

    vector<Planet> planets;
    while(locationIn >> tempRow >> tempCol >> tempSymbol >> tempId){
        if(tempRow >= 1 && tempRow <= rows && tempCol >= 1 && tempCol <= cols){
            Planet temp;
            temp.row = tempRow;
            temp.col = tempCol;
            temp.symbol = tempSymbol;
            temp.id = tempId;

            planets.push_back(temp);
        }
        else{
            cout << tempId << " out of range - ignoring" << endl;
        }
    }

    string tempName;
    while(nameIn >> tempId >> tempName){
        for(int i=0; i<planets.size(); i++){
            if(tempId == planets.at(i).id){
                while(tempName.find("XX")!= string::npos){
                    tempName.erase(tempName.find("XX"), 2);
                }
                while(tempName.find("_")!= string::npos){
                    tempName.replace(tempName.find("_"), 1, " ");
                }
                planets.at(i).name = tempName;
            }
        }
    }

    nameIn.close();
    locationIn.close();
    //now "supposedly" have completed struct of planets

    ofstream fout("journey.txt");

    vector< vector<string> > grid = makeGrid(rows,cols);
    fillVector(grid);
    grid[startRow-1][startCol-1] = "S";
    grid[endRow-1][endCol-1] = "E";
    for(int i=0; i<planets.size(); i++){
        grid[planets[i].row-1][planets[i].col-1] = planets[i].symbol;
    }

    vector<Planet> path = getShortestPath(startRow, startCol, endRow, endCol, planets);

    for(int i=0; i<grid.size(); i++){
        for(int j=0; j<grid.at(i).size(); j++){
            fout << grid[i][j];
        }
        fout << endl;
    }
    fout << "Start at " << startRow << " " << startCol << endl;
    for(int i=0; i<path.size(); i++){
        fout << "Go to " << path.at(i).name << " at " << path.at(i).row << " " << path.at(i).col <<  endl;
    }
    fout << "End at " << endRow << " " << endCol;

    fout.close(); 

    return 0;


}








void printPlanet(Planet p){
    cout << "Planet name: " << p.name << endl;
    cout << "Planet row: " << p.row << endl;
    cout << "Planet col: " << p.col << endl;
    cout << "Planet symbol: " << p.symbol << endl;
    cout << "Planet id: " << p.id << endl;
}

void printVector(vector<Planet> p){
    for(int i=0; i<p.size(); i++){
        cout << p.at(i).name << " ";
    }
    cout << endl;
}

vector< vector<string> > makeGrid(int rows, int cols){
    vector< vector<string> > map;
    for(int i=0; i<rows; i++){
        vector<string> temp(cols);
        map.push_back(temp);
    }
    return map;
}

void fillVector(vector< vector<string> > &grid){
    for(int i=0; i<grid.size(); i++){
        for(int j=0; j<grid.at(i).size(); j++){
            grid[i][j] = ".";   
        }
    }   
}

void printVectorGrid(vector< vector<string> > grid){
    for(int i=0; i<grid.size(); i++){
        for(int j=0; j<grid.at(i).size(); j++){
            cout << grid[i][j] << " ";   
        }
        cout << endl;
    }
}

2 个答案:

答案 0 :(得分:1)

  

唯一的问题是,由于某些原因,当我将文件提交到网站时,我的文件没有编译。

以下是在Linux上使用g++-4.8.4编译源代码时出现的一组错误:

$ g++ -Wall -c t.cc
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from t.cc:1:
/usr/include/c++/4.8/bits/stl_iterator_base_types.h: In instantiation of ‘struct std::iterator_traits<Planet>’:
/usr/include/c++/4.8/bits/stl_iterator_base_funcs.h:114:5:   required by substitution of ‘template<class _InputIterator> typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator) [with _InputIterator = Planet]’
t.cc:37:48:   required from here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:165:53: error: no type named ‘iterator_category’ in ‘struct Planet’
       typedef typename _Iterator::iterator_category iterator_category;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:166:53: error: no type named ‘value_type’ in ‘struct Planet’
       typedef typename _Iterator::value_type        value_type;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:167:53: error: no type named ‘difference_type’ in ‘struct Planet’
       typedef typename _Iterator::difference_type   difference_type;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:168:53: error: no type named ‘pointer’ in ‘struct Planet’
       typedef typename _Iterator::pointer           pointer;
                                                     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:169:53: error: no type named ‘reference’ in ‘struct Planet’
       typedef typename _Iterator::reference         reference;
                                                     ^
t.cc: In function ‘int main()’:
t.cc:87:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<planets.size(); i++){
                                     ^
...

第一个问题来自您对distance的定义 - 请参阅std::distance的文档。我建议你摆脱非感性的“第一个单词小写;然后是驼峰式”命名约定。

确切的问题是这个电话:

 if(minDist >= distance(temp,v.at(i))){

可以匹配您的distance(Planet &a, Planet &b) std::distance()

第二个问题(第87行)是int不适合比较的类型(请注意,这仅仅是一个警告,但是您的老师可能正在使用-Werror标志进行编译警告错误)。请改用size_t

答案 1 :(得分:-1)

我认为这与变量Locations Filename有关。您上传的网站是否有输入文件选项,如果有,请尝试一下。我尝试在一个在线shell中运行你的代码,它编译得很好,但后来它询问了文件位置,我相信这可以帮助你解决问题。