2015-12-20 91 views
-1

我想将一个变量从一个函数传递给另一个函数。我试过这种方法,但它不适合我:从其他函数中获取函数的变量C++

int c(){ 
    int x1,x2,y2,y1; 
    system("cls"); 
    cout<<"Insert Value"<<endl 
    cin>>x1; 

    return x1; 
} 

int cd() 
{ 
    int a; 
    a=c(); 
    cout<<"X1: "<<a; 
} 

任何帮助表示赞赏。谢谢!

+0

压痕,它很重要。 – Borgleader

+0

请向我们展示正在使用此类的类或代码。在这一点上,我只看到两种方法,并没有什么叫它们。 – Nate

+0

冲洗很重要。 –

回答

1

您的代码存在一些问题。

首先,您在c()函数中的cout语句后缺少分号。

此外,您还指出功能cd()应返回int但您没有返回任何东西。最后,除非你明确地调用它们,否则这些函数不会开始执行。

试试这个:

#include <iostream> 

using namespace std; 

int c(){ 
    int x1,x2,y2,y1; 

    cout<<"Insert Value"<<endl; 
    cin>>x1; 

    return x1; 
} 

int cd(){ 
    int a; 
    a=c(); 
    cout<<"X1: "<<a; 
    return a; 

} 

int main() 
{ 
    int x=cd(); //call the function to create the side effects 

    return 0; 
} 
+0

不,我已经证明了为什么消除他的编译时错误(缺少分号和函数返回值)可以解决问题。糟糕,看起来你删除了你的评论 – ForeverStudent