2015-04-02 140 views
-1

由于某种原因我得到“进程终止状态-1073741819”错误,每当我运行我的程序,我读过一些人因为错误而得到这个错误用代码块/编译器,我只想知道在我重新安装编译器之前是否有任何代码有问题。我使用的是code :: blocks和GNU GCC编译器。“进程终止状态-1073741819”简单程序与向量

我的代码创建了一个存储一个星期40个工作小时的向量,并在该向量中存储了一个向量,该向量存储代表这些小时中可用的5个人的字母。

Schedule.cpp:

#include <iostream> 
#include "Schedule.h" 
#include <vector> 
#include <string> 

using namespace std; 

/// Creates a Vector which holds 40 items (each hour in the week) 
/// each item has 5 values (J A P M K or X, will intialize as J A P M K) 

vector< vector<string> > week(40, vector<string> (5)); 

Schedule::Schedule(){ 
     for (int i = 0; i<40; i++){ 
      week[i][0] = 'J'; 
      week[i][1] = 'A'; 
      week[i][2] = 'P'; 
      week[i][3] = 'M'; 
      week[i][4] = 'K'; 
     } 
     // test 
     cout << week[1][3] << endl; 
    } 

头文件:

#ifndef SCHEDULE_H 
#define SCHEDULE_H 
#include <vector> 
#include <string> 

using namespace std; 

class Schedule 
{ 
    public: 
     Schedule(); 
    protected: 
    private: 
     vector< vector<string> > week; 

}; 

#endif // SCHEDULE_H 

main.cpp中:

#include <iostream> 
#include "Schedule.h" 
#include <vector> 
#include <string> 

using namespace std; 

int main() 
{ 
    Schedule theWeek; 
} 
+0

使用GDB并运行它,然后使用回溯。 – Cinch 2015-04-02 08:39:22

+0

'vector > week(40,vector (5));' - 这会创建一个名为week的全局矩阵变量(不要与Schedule :: week混淆)。这是故意的吗?我假设你想在构造函数中初始化成员变量;如果是这样,你应该在构造函数中写'this-> week',或者删除/重命名全局变量声明,或者将全局声明放在名称空间中。 – utnapistim 2015-04-02 08:49:17

回答

3

这不是一个copiler错误。

您的构造函数中出现内存错误。

你的代码有几个错误,例如在你的cpp中声明了一个全局向量星期,然后它在构造函数中被隐藏,因为构造函数将访问Schedule :: week。

你的CPP应该是这样的:

week[i][0] = 'J' ; 

此刻的你:

// comment out the global declaration of a vector week ... 
// you want a vector for each object instantiation, not a shared vector between all Schedule objects 
// vector< vector<string> > week(40, vector<string> (5)); 


Schedule::Schedule() 
{ 
    for (int i=0;i<40;i++) 
    { 
     vector<string> stringValues; 
     stringValues.push_back("J"); 
     stringValues.push_back("A"); 
     stringValues.push_back("P"); 
     stringValues.push_back("M"); 
     stringValues.push_back("K"); 
     week.push_back(stringValues); 
    } 
} 

当您尝试访问您的周向量首次您得到您的代码中的内存故障调用这行代码,你的Schedule :: week向量中有0个元素(所以week [i]已经是错误了)。