小编Max*_*ard的帖子

C ++成员参考基本类型'Vertex * const'不是结构或联合

尝试访问存储在向量中的对象的方法时遇到麻烦。我知道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)

c++ iterator const reference vector

2
推荐指数
1
解决办法
5638
查看次数

C++中的重新定义,头文件中的先前定义?

所以我很困惑.尝试实现先前在头文件中声明的方法时,我收到重新定义错误.我在标题中添加了包含防护,但仍然遇到了同样的错误.如果有人能向我解释我没有看到的东西,那就太棒了.

在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)

c++ header include guard redefinition

0
推荐指数
1
解决办法
3838
查看次数

标签 统计

c++ ×2

const ×1

guard ×1

header ×1

include ×1

iterator ×1

redefinition ×1

reference ×1

vector ×1