2017-02-16 150 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)) ...