gpt4 book ai didi

c++ - 无效函数导致编译器错误 "variable or field ‘funcName’ 声明无效”

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:56 26 4
gpt4 key购买 nike

我已经为我在头文件中声明的类的方法声明了一个辅助函数,但出于某种原因,当我编译源代码文件时,我收到一条错误消息,告诉我我将一个变量或字段声明为 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 是私有(private)的。

额外提示:从 .h 文件中删除 using 指令,而是限制您对 string 等的所有使用。考虑包括许多 cpp 文件中的那个 header 。

关于c++ - 无效函数导致编译器错误 "variable or field ‘funcName’ 声明无效”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7750116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com