2011-04-02 55 views
0

這裏(的一部分),我的代碼:錯誤:函數未在此範圍內聲明。幫幫我?

... 

Node<char*>* nodes[count2];//array of pointers to last level 
    nodes[0] = f1.rootPtr; 
    processInput(input, f1.rootPtr, nodes, 0, count2); 
    //I get an error that says this function is not declared in this scope. 

    return input; 

} 

void processInput(istream& input, Node<char*>* node, Node<char*>** nodeArray, 
     int level, int& count) 
{ 
    //variables 
    Node<char*>* aNode = new Node<char*>(); 
    char charArray[150]; 
... 

當我運行程序我得到這個:

Forest.cpp: In function 'std::istream& operator>>(std::istream&, Forest<char*>&)': 
Forest.cpp:93:53: error: 'processInput' was not declared in this scope 
make[2]: *** [build/Debug/MinGW-Windows/Forest.o] Error 1 
make[1]: *** [.build-conf] Error 2 

這裏是頭文件的一部分:

template<typename NODETYPE> class Forest{ 

    /* 
    * builds a forests consisting of the first and second forest reference 
    */ 
    template<NODETYPE> 
    friend Forest& operator+(Forest<NODETYPE>& f1, Forest<NODETYPE>& f2); 

    /* 
    * insert into the output stream a preorder traversal of the input forest 
    */ 
    template<NODETYPE> 
    friend ostream& operator<<(ostream& ostr, const Forest<NODETYPE>& f1); 

    /* 
    * extracts a forest from the input stream and builds it for the forest argument variable name 
    */ 
    //template<NODETYPE> 
    friend istream& operator>>(istream& file, Forest<char*>& f1); 

    /* 
    *Used with istream to go through input 
    */ 
    //template<NODETYPE> 
    void processInput(istream& input, Node<char*>* nodeBefore, Node<char*> ** nodeArray, 
     int levelBefore, int& count); 

public: 
    Forest(){ 

.. 

什麼我做錯了嗎?我爲什麼得到這個錯誤。有什麼建議?

謝謝!

編輯:

我試過你說的,但它仍然無法正常工作。我使用模板,所以也許這就是我的問題在哪裏?

頁眉:

模板//我應該保持此?當我拿出來它也不起作用 朋友istream &運營商>>(istream &文件,森林& f1);

私人:
空隙processInput(istream的&輸入,節點*節點,節點** nodeArray, INT水平,整數&計數);

.cpp文件:

模板 istream的&操作>>(istream的&輸入,森林& F1){// 代碼 ... processInput(輸入,f1.rootPtr,節點,0 ,count2); //錯誤:無法解析標識符processInput }

/** * 處理輸入 / 空隙森林:: processInput(istream的&輸入,節點節點,節點** nodeArray,INT水平, int & count){ //代碼

再次感謝。

回答

0

你需要在你的類函數前加上他們班的名字,你現在這樣做嗎?如果不是,編譯器認爲它們是自由函數。

例如,

void processInput(istream& input, Node<char*>* node, Node<char*>** nodeArray, 
    int level, int& count) { 
    // ...code... 
} 

應該

void Forest::processInput(istream& input, Node<char*>* node, Node<char*>** nodeArray, 
    int level, int& count) { 
    // ...code... 
} 
+0

當然,只有定義,而不是類本身內部的聲明。 – Xeo 2011-04-02 00:52:27

+0

試過了......現在我收到另一個錯誤: – Joey 2011-04-02 01:05:06

+0

@Xeo:肯定。我編輯,使其更清晰。 – Jon 2011-04-02 01:05:13