切换到头文件/实现/主文件后的C ++分段错误

时间:2016-04-07 08:32:15

标签: c++

我得到了一个"分段错误(核心转储)"运行时错误,代码如下: 它在一个.pp文件中运行良好,但是当我将它分成标题/实现/主文件时,它会编译,但我得到分段错误错误。 更新*

#include <cctype>
#include <iostream>
#include <stdlib.h>

using namespace std;

#include "./mult_div.h"

int main(int arg, char *argv[] ){
    int diff=1;
    while(diff==1){

    bool check=1;
    char *argv2[1];
    char *argv3[1];
    argv2[0]=argv[1];
    argv3[0]=argv[2];
    cout<< argv2[0] << argv3[0]<<endl <<endl;
    while(check){
            cout<<"call bool"<<endl;
            if(is_valid_dimensions(*argv2, *argv3)){
                    check = false;
    }
            else
                    check=true;
    }


    cout<<*argv2[0]<<" "<<*argv3[0]<<endl;

    int rows= atoi(argv[1]);
    int cols= atoi(argv[2]);

    cout<< rows <<" " <<  cols<< endl;

    mult_div_values **table;
    table = create_table(rows, cols);
    set_mult_values(table, rows, cols);
    set_div_values(table, rows, cols);

    print_mult_table(table, rows, cols);

    print_div_table(table, rows, cols);

    delete_table(table, rows, cols);
    cout<<"Would you like to see a different size matrix(0-no, 1-yes)?" << " ";
    cin>>diff;
    if(diff == 1){
    cout<<"Enter a new size matrix:"<<endl;
    cin>> argv[1] >> argv[2];
    }

    }

实施

mult_div_values** create_table(int m, int n){
mult_div_values **table = new mult_div_values *[m];
for( int i=0; i<m; i++){
        table[i] = new mult_div_values [n];
}
return table;
}
void set_mult_values(mult_div_values **table, int m, int n){
for (int i=1; i<=m; i++){
        for (int j=1; j<=n; j++){
                table[i-1][j-1].mult = i*j;
        }
}
}
void set_div_values(mult_div_values **table, int m, int n){
for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++){
                table[i-1][j-1].div =(float) i/(float) j;
        }
}
}
void print_mult_table(mult_div_values **table, int m,int n){
cout<< "multiplication\n";
 for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                    cout << table[i][j].mult << " ";
            }
            cout << endl;
    }
}


void print_div_table(mult_div_values **table, int m, int n){
cout<<" division\n";
for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
               cout<< table[i][j].div << " ";
        }
        cout << endl;
}}

void delete_table(mult_div_values **table, int m, int n){
for(int i=0; i<m; i++){
        delete[] table[i];
}
delete[] table;}}  

0 个答案:

没有答案