'Class Instance Object' 未在此范围内声明

'Class Instance Object' was not declared in this scope

我是 C++ 的新手,我真的很难尝试定义 class ConflictGraph 的单个实例并从另一个 class 中的不同方法访问它的方法] ConvexHullBuilder.

由于问题的原因,我需要在ConvexHullBuilder::computeConvexHull()中的某个点上初始化ConflictGraph,当我有一些顶点和一个dcel时,我需要调用ConflictGraph's initialize() 以正确初始化 ConflictGraph。这很好用。

当我尝试在 ConvexHullBuilder::finalizeConvexHull.

中调用 ConflictGraph 的 lookForVisibleFaces() 时出现问题

我收到以下错误

error: ‘conflictGraph’ was not declared in this scope

facesVisible = conflictGraph.lookForVisibleFaces(remainingVertices[i]);

                 ^

类型有点坑,不过不用注意。我只需要深入了解如何在内部 class 方法

中访问外部 class 方法

ConflictGraph.h:

class ConflictGraph{
public:
    ConflictGraph(DrawableDcel* dcel, std::vector<Dcel::Vertex*> vertices);
    void initializeConflictGraph();
    std::set<Dcel::Face*>* lookForVisibleFaces(Dcel::Vertex*);
private:
    DrawableDcel *dcel;
    std::vector<Dcel::Vertex*> tetrahedronVertices;

    void checkVisibility();

}

ConflictGraph.cpp:

#include "conflictgraph.h"

/**
 * @brief ConflictGraph::ConflictGraph() Constructor
 * @params takes dcel and tetrahedron vertices as input
 */
ConflictGraph::ConflictGraph(DrawableDcel* dcel, std::vector<Dcel::Vertex*> tetrahedronVertices){
    this->dcel = dcel;
    this->tetrahedronVertices = tetrahedronVertices;
}

/**
 * @brief ConflictGraph::initializeConflictGraph() initializes the conflict graph
 */
void ConflictGraph::initializeConflictGraph(){
    //Check which faces see which vertices and viceversa
    checkVisibility();//Works
}

/**
 * @brief lookForVisibleFaces(Dcel::Vertex* vertex) finds which faces are visible from a given vertex
 * @param VERTEX vertex given vertex
 * @retuns map of visible faces and passed Vertex
 */
std::set<FACE>* ConflictGraph::lookForVisibleFaces(Dcel::Vertex* vertex){
 . . .
}

ConvexHullBuilder.h:

#include "conflictgraph.h"

class ConvexHullBuilder{

public:
    ConvexHullBuilder(DrawableDcel* dcel);
    void computeConvexHull();

private:
    DrawableDcel *dcel;
    void finalizeConvexHull(std::vector<Dcel::Vertex*);
};

ConvexHullBuilder.cpp

#include "convexhullbuilder.h"

/**
 * @brief ConvexHullBuilder::ConvexHullBuilder() Conmstructor
 * @params takes dcel as input
 */
ConvexHullBuilder::ConvexHullBuilder(DrawableDcel* dcel){
   this->dcel = dcel;
}

/**
 * @brief ConvexHullBuilder::computeConvexHull() takes dcel as input.
 *        Starts the algorithm calling all the different functions needed.
 *        Adds Vertices, Builds a Tetrahedron . . .
 */
void ConvexHullBuilder::computeConvexHull(){
. . .
/** VerticesForCG is an array of pointers to vertex, properly filled
    allVertices contains all the remaining vertices**/
//Initializes Conflict Graph with Dcel And First 4 Vertices
    ConflictGraph conflictGraph = ConflictGraph(dcel, verticesForCG);
    conflictGraph.initializeConflictGraph(); //Works

    //Loop through remaining vertices
    finalizeConvexHull(allVertices);//Does not Work

}

/**
 * @brief ConvexHullBuilder::finalizeConvexHull starts last phase to build the convex hull
 * @param VERTEX_POINTERS_LIST remainingVertices i=4 -> n vertices
 */
void ConvexHullBuilder::finalizeConvexHull(std::vector<Dcel::Vertex* remainingVertices){

    //Loop through remaining vertices
    for(unsigned int i=4; i<remainingVertices.size(); i++){

       //Initializing faces visible by a vertex
       std::set<Dcel::Face*>* facesVisible;

      //Check Which faces sees i-Vertex and assigning them
      facesVisible = conflictGraph.lookForVisibleFaces(remainingVertices[i]);//Error

    }
}

编辑

我也试过将 ConflictGraph 实例传递给 ConvexHullBuilder.h 就像

private:
    DrawableDcel *dcel;
    ConflictGraph *conflictGraph;

ConvexHullBuilder.cpp中:

{
  //Initializes Conflict Graph with Dcel And First 4 Vertices
  this->conflictGraph = new ConflictGraph(dcel, verticesForCG);
  conflictGraph->initializeConflictGraph();

  //Loop through remaining vertices
  finalizeConvexHull(allVertices);
}

{
  facesVisible = conflictGraph->lookForVisibleFaces(remainingVertices[i]);
}

但是在 ConvexHullBuilder.h 我得到:

ConflictGraph does not name a type

我试着查看有关 SO 的其他答案,但无法解决我的问题。

是什么导致了这个错误?我怎样才能防止这种情况发生?如何在内部 class 不同的方法上使用不同的外部 class 方法?

您正在尝试在 finalizeConvexHull 中使用 cinflictGraph。但是,它已在 computeConvexHull 中被清除。成员函数之间不共享本地资源。

解法:

一个可能的解决方案是在不深入思考系统的情况下将 conflictGraph 声明为 class ConvexHullBuilder 的私有成员变量。所以,你可以在两个成员函数中使用它。

conflictGraph 在方法 ConvexHullBuilder::computeConvexHull() 中声明为局部变量,因此它仅在调用该函数时存在。并且只能在该函数内访问。 finalizeConvexHullcomputeConvexHull 调用,因此 conflictGraph 至少在调用时存在,但为了 finalizeConvexHull 能够访问它,您需要将其作为参数传递给该函数。

变量的范围始终是它所在的块。

您需要在第二个函数中声明您的 conflictGraph

您需要申报

private:
ConflictGraph conflictGraph;

在 "ConvexHullBuilder.h" 文件中

并在 "ConvexHullBuilder.cpp" 文件中声明为:

ConvexHullBuilder::ConvexHullBuilder(DrawableDcel* dcel){
   this->dcel = dcel;
   conflictGraph = ConflictGraph(dcel, verticesForCG);
}

然后你可以在 "ConvexHullBuilder.cpp" 文件

中的任何函数中使用 conflictGraph 对象

我设法解决了这个问题。它是由两个 class 中的相同 includes 引起的。我只需要在 class includedheaderinclude 某个 module。例如,我必须只在 ConflictGraph.h 而不是在 ConvexHullBuilder.

上做包含

以下是我如何从 ConvexHullBuilder 函数内部访问 ConflictGraph 的不同方法

ConvexHullBuilder.h:

private:
   . . .
   ConflictGraph *conflictGraph;

ConvexHullBuilder.cpp:

void ConvexHullBuilder::computeConvexHull(){

  conflictGraph = new ConflictGraph(dcel, params);

  conflictGraph->initializeConflictGraph();
  finalizeConvexHull(parameters);

}

void ConvexHullBuilder::finalizeConvexHull(myType parameters){

  conflictGraph->lookForVisibleFaces(someParams);
}