【发布时间】: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