【发布时间】:2023-03-05 19:36:01
【问题描述】:
我知道 setter 函数在 C++ 中是基本的,但我似乎无法正确创建一个。
我有一个类Graph,其中包含另一个类Vertex。
在main 中,我实例化了一个Graph 类型的对象,并使用该对象来读取一个将Vertex 类型的对象添加到Graph 的文件。
问题是,之后当我尝试打印出我的Graph 对象时,一切似乎都是空的。我认为我在滥用指针或引用。
你能解释一下为什么我有 6 个 Vertex 对象,但它们都是空的吗? (我试图将Vertex 的vector 更改为vector* 但这并没有解决我的问题,我想我还是丢失了Vertex 对象......)?
Main.cpp
int main(int argc, char *argv[]){
Graph g = Graph();
g.readDotFile("graph.dot");
std::cout << g << std::endl;
return 0;
}
Graph.hpp
class Graph{
private:
class Vertex{
public:
Vertex();
Vertex(std::string name);
Vertex(Vertex const& v);
~Vertex();
const std::string& getIdVertex()const;
friend std::ostream& operator<<(std::ostream &os, Vertex const& v);
void printVertex(std::ostream &os)const;
void setStartToStart(const std::string& name);
private:
std::string idVertex;
std::vector<std::string> startToStart;
};
public:
Graph();
void readDotFile(std::string dotFile); // Graph from a dot file
Graph(Graph const& g);
~Graph();
void addVertex(Vertex& v);
void addEdge(int typeOfEdge, std::string name1, std::string name2);
const std::vector<Graph::Vertex>& getVertices()const;
friend std::ostream& operator<<(std::ostream &os, Graph const& g);
friend std::ostream& operator<<(std::ostream &os, Vertex const& v);
void printGraph(std::ostream &os)const;
private:
std::vector<Vertex> vertices;
};
Graph.cpp
#include "graph.hpp"
Graph::Vertex::Vertex(){}
Graph::Vertex::Vertex(std::string name){
idVertex = name;
}
Graph::Vertex::Vertex(Graph::Vertex const& v){}
Graph::Vertex::~Vertex(){}
const std::string& Graph::Vertex::getIdVertex()const{ return idVertex; }
void Graph::Vertex::setStartToStart(const std::string& v){
startToStart.push_back(v);
}
Graph::Graph(){}
Graph::Graph(Graph const& g){}
Graph::~Graph(){}
const std::vector<Graph::Vertex>& Graph::getVertices()const{ return vertices; }
void Graph::readDotFile(std::string file){
std::ifstream dotFileIn(file.c_str());
std::string line;
while (getline(dotFileIn, line)){
// Read the file, the problem don't come from here, I just don't show the code
addEdge(1, "origin", "destination");
}
}
}
void Graph::addVertex(Graph::Vertex& v){ // Before create, verify the vertex does not exist
vertices.push_back(v);
}
void Graph::addEdge(int typeOfEdge, std::string name1, std::string name2){
Graph::Vertex v1 = Graph::Vertex(name1);
addVertex(v1);
Graph::Vertex v2 = Graph::Vertex(name2);
addVertex(v2);
v1.setStartToStart(name2);
}
std::ostream& operator<<(std::ostream &os, Graph const& g)
{
g.printGraph(os);
return os;
}
void Graph::printGraph(std::ostream &os)const{
for (unsigned int i = 0; i < vertices.size(); ++i){
vertices[i].printVertex(os);
}
}
std::ostream& operator<<(std::ostream &os, Graph::Vertex const& v)
{
v.printVertex(os);
return os;
}
void Graph::Vertex::printVertex(std::ostream &os)const{
os << " vertex : " << idVertex << "\t";
for (unsigned int i = 0; i < startToStart.size(); ++i){
os << "startToStart: " << startToStart[i] << "\t";
}
}
}
【问题讨论】:
-
只是我的对象中没有任何内容。当我打印我的图表时,我有:6Vertex:Vertex:(->但这里什么都没有,它不显示我的顶点 ID 或我的边列表)我的设置函数或我在类 Graph 和类 Vertex 之间的链接是错误的
-
除非您显着减小已发布代码的大小,否则我不会期望任何帮助。删除任何不必要的东西,简化然后向我们展示更简单的代码。也许即使在这个过程中你也会找到解决方案。
标签: c++ class methods graph private