【问题标题】:Trying to submit a program to a teachers grading website试图向教师评分网站提交程序
【发布时间】:2016-12-12 02:31:10
【问题描述】:

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

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

【问题讨论】:

  • 你和我对完美工作有不同的看法。我认为分段错误不能完美地工作,但这只是我。这里确实没有足够的信息来回答你的问题。我不确定您在问什么:为什么它在本地有效,但在我们不熟悉的某些网站上无效,或者为什么您会遇到分段错误。您可以edit您的问题澄清任何信息。
  • 如果不能编译,请贴出编译错误;网站肯定必须显示吗?还是你混淆了编译和执行?
  • 我理解其中的区别,它再次在我的本地设备上编译得很好,但是一旦我将它上传到评分网站,它就会说文件没有编译。我只是想知道我是否在某个地方搞砸了
  • 很抱歉,您不是要我们查找您的语法错误吗?网站真的不给你编译错误吗?您应该找出编译器的版本和设置,以便您可以在本地以完全相同的方式编译它。如果是 GCC,你应该至少使用-Wall -Werror,并且可能使用-Wextra
  • 这是this question的副本。

标签: c++ compilation segmentation-fault


【解决方案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 &amp;a, Planet &amp;b) std::distance()

第二个问题(第 87 行)是 int 不是比较合适的类型(请注意,这只是一个警告,但您的老师可能在编译时使用 -Werror 标志,它将警告升级为错误) .请改用size_t

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 网站基本上是一个拖放的情况,我将文件拖到一个盒子上,它会自动上传到网站。我尝试上传它,它一直告诉我它没有编译:(
    猜你喜欢
    • 2017-10-19
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    相关资源
    最近更新 更多