我对C ++很陌生,我真的很想尝试定义一个类ConflictGraph
的单个实例,并从另一个类ConvexHullBuilder
内的不同方法访问它的方法
出于问题的原因,我需要在ConflictGraph
中的某个点初始化ConvexHullBuilder::computeConvexHull()
,当我有一些顶点和dcel
时,我需要调用ConflictGraph's initialize()
正确初始化ConflictGraph。这很好。
当我尝试在lookForVisibleFaces()
内调用ConflictGraph ConvexHullBuilder::finalizeConvexHull
时出现问题。
我收到以下错误
错误:'conflictGraph'未在此范围内声明
facesVisible = conflictGraph.lookForVisibleFaces(remainingVertices [i]);
^
这些类型有点棘手,但不要注意它们。我只需要了解如何在内部类方法中访问外部类方法
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
未命名类型
我试着看看SO上的其他答案,但我无法解决他们的问题。
导致此错误的原因是什么?我怎么能阻止这个?如何在内部类不同的方法上使用不同的外部类方法?
答案 0 :(得分:1)
您正试图在cinflictGraph
内使用finalizeConvexHull
。但是,它在computeConvexHull
中被删除了。成员函数不会在彼此之间共享本地资源。
<强>解决方案:强>
在不深入思考系统的情况下,一种可能的解决方案是将conflictGraph
声明为类ConvexHullBuilder
的私有成员变量。因此,您可以在两个成员函数中使用它。
答案 1 :(得分:1)
namespace App\Engine\Threads\ThreadClass;
use Illuminate\Support\Facades\Log;
class TestThread extends \Thread
{
private $threadID;
public function __construct($threadID)
{
$this->threadID = $threadID;
}
/**
* @return mixed
*/
public function getThreadID()
{
return $this->threadID;
}
public function run()
{
$this->threadID = $this->threadID.''.mt_rand(4000, 5000);
}
}
在方法conflictGraph
中声明为局部变量,因此它仅在调用该函数时存在。并且只能在该功能中访问。 ConvexHullBuilder::computeConvexHull()
会调用finalizeConvexHull
,因此computeConvexHull
至少在通话时存在,但为了让conflictGraph
能够访问它,您需要将它作为参数传递给该函数。
答案 2 :(得分:1)
变量的范围始终是它内部的块。
您需要在第二个函数中声明conflictGraph
。
答案 3 :(得分:1)
您需要声明
private:
ConflictGraph conflictGraph;
“ConvexHullBuilder.h”文件中的
并在“ConvexHullBuilder.cpp”文件中声明为:
ConvexHullBuilder::ConvexHullBuilder(DrawableDcel* dcel){
this->dcel = dcel;
conflictGraph = ConflictGraph(dcel, verticesForCG);
}
然后你可以在“ConvexHullBuilder.cpp”文件中的任何函数中使用conflictGraph对象
答案 4 :(得分:0)
我设法解决了这个问题。它是由两个类中的includes
引起的。我只需要include
module
个header
个included
。例如,我必须仅在ConflictGraph.h
而不是在ConvexHullBuilder
中执行包含。
以下是我能够从ConflictGraph
内部ConvexHullBuilder
函数中访问private:
. . .
ConflictGraph *conflictGraph;
的不同方法
<强> ConvexHullBuilder.h 强>:
void ConvexHullBuilder::computeConvexHull(){
conflictGraph = new ConflictGraph(dcel, params);
conflictGraph->initializeConflictGraph();
finalizeConvexHull(parameters);
}
void ConvexHullBuilder::finalizeConvexHull(myType parameters){
conflictGraph->lookForVisibleFaces(someParams);
}
<强> ConvexHullBuilder.cpp 强>:
pdfFileText.replaceAll("\\s+", " ").contains(institution)