2011-10-13 159 views
1

我已经声明了一个辅助函数给我在头文件中声明的类的方法,并且由于某种原因,当我编译源代码文件I得到一个错误告诉我,我宣布一个变量或字段为void。我不知道如何解释这一点,因为我的目标是将该功能宣布为无效。void函数导致编译器错误“变量或字段'funcName'声明为void”

编译器误差如下:

k-d.cpp:10: error: variable or field ‘insert_Helper’ declared void 
k-d.cpp:10: error: ‘node’ was not declared in this scope 
k-d.cpp:10: error: ‘root’ was not declared in this scope 
k-d.cpp:10: error: expected primary-expression before ‘*’ token 
k-d.cpp:10: error: ‘o’ was not declared in this scope 
k-d.cpp:10: error: expected primary-expression before ‘int’ 

线10的在下面的代码的等效物是线5.

的源代码如下:

#include <iostream> 
#include "k-d.h" //Defines the node and spot structs 
using namespace std; 

void insert_Helper(node *root, spot *o, int disc) { 
    (...Some code here...) 
} 

void kdTree::insert(spot *o) { //kdTree is a class outlined in k-d.h 
    insert_Helper(root, o, 0); //root is defined in k-d.h 
} 

如果任何人都可以发现任何会导致编译器不会将其视为函数的东西,这将不胜感激。谢谢!

P.S.我没有把它标记为kdtree文章,因为我非常肯定解决方案不依赖于代码的这个方面。

更新:

这里是kd.h:

#ifndef K_D_H 
#define K_D_H 

// Get a definition for NULL 
#include <iostream> 
#include <string> 
#include "p2.h" 
#include "dlist.h" 

class kdTree { 
    // OVERVIEW: contains a k-d tree of Objects 

public: 

    // Operational methods 

    bool isEmpty(); 
    // EFFECTS: returns true if tree is empy, false otherwise 

    void insert(spot *o); 
    // MODIFIES this 
    // EFFECTS inserts o in the tree 

    Dlist<spot> rangeFind(float xMax, float yMax); 

    spot nearNeighbor(float X, float Y, string category); 

    // Maintenance methods 
    kdTree();         // ctor 
    ~kdTree();         // dtor 

private: 
    // A private type 
    struct node { 
     node *left; 
     node *right; 
     spot *o; 
    }; 

    node *root; // The pointer to the 1st node (NULL if none) 
}; 

#endif 

而且p2.h:

#ifndef P2_H 
#define P2_H 
#include <iostream> 
#include <string> 
using namespace std; 

enum { 
    xCoor = 0, 
    yCoor = 1 
}; 

struct spot { 
    float key[2]; 
    string name, category; 
}; 

#endif 
+3

向我们展示“k-d.h”。 – cnicutar

+0

您需要发布足够的代码示例来演示该问题。尝试将它隔离到发生错误时的绝对最小情况--10个中有9个,这将为您解决问题,如果没有,您可以发布它,并且有人应该很快发现错误。 – Ayjay

+0

请提供一个我们可以用来重现错误的例子,如[这里](http://sscce.org)所述。 –

回答

0

首先,您需要限定kdTree::node,因为它被声明为内部结构。其次,你必须让insert_Helper成为你班级的成员,因为node是私人的。

额外提示:从.h文件中删除using指令,和相当资格的string所有的使用等考虑在很多cpp文件头。

+0

如果他让它成为班级的成员,那么他就不需要去限制它。 –

+0

我想我会这样做,而不是公开节点。我试图将其作为实施的“隐藏”部分。 – smitty

+0

感谢您对“使用”的建议,我应该摆脱那种习惯。 – smitty

0

node是内kdTree嵌套式,在函数定义,你必须将其命名为kdTree::node。但是,由于node是私密的,因此您也必须做些什么。

+0

我正试图在课堂上保留那个,但我认为它必须是公众成员。感谢您的建议,我讨厌当我遇到这样的愚蠢的错误: - – smitty

+0

的意思是在那里把笑脸,但按下输入键,而不是轮班大声笑 – smitty

+0

@smitty:你不一定要公开,它看起来你的班级可以使用朋友。 –

相关问题