2012-11-03 135 views
0

我有一個函數聲明類似R6010:我可以將一個int&傳遞給一個接受int的函數嗎?

void func1(int& x) { 
    func2(x); // func2 accepts an int 
} 

我認爲這是導致程序崩潰?我得到的錯誤

R6010 - abort() has been called

什麼我需要做X傳遞到接受一個int的功能?我希望他們的工作一樣的...因爲我可以只使用回聲cout << x

UPDATE

只是一個測試x的值:

cout << stmtNo << endl; 
Node* n = ast->getNode(stmtNo); 
cout << n->getNodeType() << " " << n->getStmtNo() << endl; 

上面的失敗......下面通過

cout << stmtNo << endl; 
Node* n = ast->getNode(1); 
cout << n->getNodeType() << " " << n->getStmtNo() << endl; 
+2

「我認爲這是程序崩潰」你調試了嗎? –

+0

是的,我把調用從'func2(x)'改爲'func2(1)',它的工作......不知道似乎是這樣的問題...壽...我不是一個'func2 ()'...並且也是C++的新手 –

+0

對func1()的調用是什麼樣的? – juanchopanza

回答

2

沒有問題,您始終可以傳遞整數引用作爲整數的參數。

整數引用可以解釋爲自動解除引用的常量指針。

#include<iostream> 
using namespace std; 
void fun_2(int s) 
{ 
    cout<<s<<endl; 
} 
void func(int &d) 
{ 
    fun_2(d); 
} 

int main() 
{ 
    int x=99; 
    func(x); 

    system("pause"); 
    return 0; 
} 

上面的代碼完美地工作!

相關問題