2017-02-16 263 views
-1

我想從一個布爾函數返回多個值,但我得到了「分段錯誤(核心轉儲)」錯誤。我的代碼是布爾返回很多值

#include<iostream> 

using namespace std; 

bool te(int b,int *c,int *e){ 

    if (b>5){ 
     *c=68; 
     return true; 
    } 
    else { 
     *e=69; 
     return false; 
    } 
} 

int main() { 
    int y; 
    int *z; 
    int *r; 

    cout<<"Give number:"<<endl; 
    cin>>y; 

    if(te(y,z,r)==1) { 
     cout<<"b is >5"<<endl; 
     cout<<*z<<endl; 
    } 
    else { 
     cout<<"b is <5"<<endl; 
     cout<<*r<<endl; 
    } 

    return 0; 
} 

它的工作原理如果bool = false,但在bool = true時出現分段錯誤。

+0

使用引用或返回'的std :: tuple'。或者你的自定義對象。 – LogicStuff

回答

1

您正在使用指針而不分配實際的物理內存。 你應該這樣做:

int y, z, r; 

然後

if (te(y, &z, &r)) ...