2011-04-02 57 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