尝试访问存储在向量中的对象的方法时遇到麻烦。我知道getEdges返回边缘的无序贴图,但是我缺少关于如何从向量中引用Vertex对象的内容。救命?
在void UndirectedGraph :: minSpanningTree()中:
std::vector<Vertex*> visited;
if(vertices.begin() != vertices.end())
{
visited.push_back(vertices.begin()->second);
visited[0]->distance = 0;
}
else
{
return;
}
std::vector<Vertex*>::const_iterator vit;
vit = visited.begin();
std::unordered_map<std::string, Edge> edges;
edges = vit -> getEdges();
Run Code Online (Sandbox Code Playgroud)
在const std :: unordered_map和Vertex :: getEdges()const中:
return edges;
Run Code Online (Sandbox Code Playgroud)
错误:
UndirectedGraph.cpp:112:21: error: member reference base type 'Vertex
*const' is
not a structure or union
edges = vit -> getEdges();
~~~ ^ ~~~~~~~~ 1 error generated.
Run Code Online (Sandbox Code Playgroud)
- 编辑 -
改变中
edges = vit -> getEdges();
Run Code Online (Sandbox Code Playgroud)
至
edges = *(vit)->getEdges(); …Run Code Online (Sandbox Code Playgroud) 所以我很困惑.尝试实现先前在头文件中声明的方法时,我收到重新定义错误.我在标题中添加了包含防护,但仍然遇到了同样的错误.如果有人能向我解释我没有看到的东西,那就太棒了.
在main.cpp包含的文件中:2:./ something.cpp:7:12:错误:重新定义'method'int Thing :: method(void)^ ./ thing.hpp:12:6:注意:之前的定义在这里int方法(void){}; ^
- 编辑 -
我现在得到以下内容:
重复符号__ZN5Thing6methodEv in:main.o thing.o ld:1个用于体系结构x86_64的重复符号
thing.hpp:
#ifndef THING_H
#define THING_H
class Thing {
public:
int a;
int b;
char c;
int method(void) {};
Thing(int anA, int aB, char aC): a(anA), b(aB), c(aC) {};
};
#endif
Run Code Online (Sandbox Code Playgroud)
thing.cpp
#include <iostream>
#include <stdio.h>
#include "thing.hpp"
using namespace std;
int Thing::method(void)
{
return 5;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <iostream>
#include "thing.cpp"
using namespace std;
Thing* thing = new Thing(5,5,'c');
int main(int argc, char …Run Code Online (Sandbox Code Playgroud)