void 函数导致编译器错误"variable or field ‘funcName’ declared void"
Void function is causing compiler error "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
文件中包含该头文件。
node
是kdTree
内的嵌套类型,在函数定义中必须将其命名为kdTree::node
。然而,由于node
是私有的,您也必须对此做一些事情。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 在c++类上调用void函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 在派生函数中指定void*参数
- C++为什么尽管我调用了void函数,它却不起作用
- 如何从void函数输出字符串
- 我应该使用什么来代替void作为变体中的替代类型之一
- 奇怪的结构&GCC&clang(void*返回类型)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 引用一个已擦除类型(void*)的指针
- 将尾部调用void(i32,..)位转换为llvm::函数以获取FnAttribute
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 如何将指针从一个void函数传递到另一个C++
- 为什么我在使用void函数时得到错误代码C2276
- void*到驱动程序中的UnicodeString
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 指针没有更新它在void函数内部指向的值
- void 函数导致编译器错误"variable or field ‘funcName’ declared void"
- 尝试定义一个函数,但我得到了"variable or field `function_A' declared void"