void 函数导致编译器错误"variable or field ‘funcName’ declared void"



我为头文件中声明的类的方法声明了一个辅助函数,由于某种原因,当我编译源代码文件时,我得到一个错误,告诉我将变量或字段声明为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
}

如果有人能发现任何可能导致编译器不将此视为函数的东西,将不胜感激。谢谢!

注:我没有把这篇文章标记为kdtree,因为我很确定解决方案不依赖于代码的这一方面。

更新:

这里是k-d - 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 

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

额外提示:从.h文件中删除using指令,并限制所有string的使用,等等。考虑在许多cpp文件中包含该头文件。

nodekdTree内的嵌套类型,在函数定义中必须将其命名为kdTree::node。然而,由于node是私有的,您也必须对此做一些事情。

相关内容

最新更新