I have declared a helper function to a method of a class I declared in a header file, and for some reason when I compile the source code file I get an error telling me that I declared a variable or field as void. I am not sure how to interpret this since my goal was for the function to be declared void.
The compiler errors are as follows:
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’
The equivalent of line 10 in the code below is line 5.
The Source Code is as follows:
#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
}
If anybody can spot anything that would cause the compiler to not see this as a function it would be greatly appreciated. Thanks!
P.S. I didn’t tag this as a kdtree post because I’m pretty sure the solution does not depend on that aspect of the code.
Update:
Here is 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
And 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
First off, you need to qualify
kdTree::nodebecause it’s declared as an inner struct. Secondly, you have to makeinsert_Helpera member of your class becausenodeis private.Extra tip: remove the
usingdirectives from the.hfiles, and rather qualify all your usage ofstring, etc. Consider including that header in a lot ofcppfiles.