Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sileno De Oliveira Brito

std::map no C++

Recommended Posts

Estou começando agora a ver o C++, estou tentando usar o std::map, mas estou tendo diversos problemas

Fiz um exemplo simples e mesmo assim não consegui compilar.

 

Arquivo .h de exemplo:

 



#ifndef PESSOA_H
#define PESSOA_H


#include <map>
using namespace std;


class Pessoa {
public:
    Pessoa();
    Pessoa(const Pessoa& orig);
    virtual ~Pessoa();
    bool addicionarFilhos(Pessoa p);


    long getId() const {
        return id;
    }


    void setId(long id) {
        this->id = id;
    }
    std::map<long, Pessoa> listarFilhos();
private:
    std::map<long, Pessoa> filhos;
    long id;
};


#endif /* PESSOA_H */

Arquivo cpp

 



#include <map>


#include "Pessoa.h"
using namespace std;


Pessoa::Pessoa() {
}


Pessoa::Pessoa(const Pessoa& orig) {
}


Pessoa::~Pessoa() {
}


bool Pessoa::addicionarFilhos(Pessoa p) {
    this->filhos.insert(p.getId(), p);
}


std::map<long, Pessoa> Pessoa::listarFilhos() {
    return this->filhos;
}

Erros de compilação:

g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/Pessoa.o.d -o build/Debug/GNU-Linux-x86/Pessoa.o Pessoa.cpp
Pessoa.cpp: In member function ‘bool Pessoa::addicionarFilhos(Pessoa)’:
Pessoa.cpp:23:37: error: no matching function for call to ‘std::map<long int, Pessoa>::insert(long int, Pessoa&)’
Pessoa.cpp:23:37: note: candidates are:
In file included from /usr/include/c++/4.7/map:61:0,
from Pessoa.cpp:8:
/usr/include/c++/4.7/bits/stl_map.h:522:7: note: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = long int; _Tp = Pessoa; _Compare = std::less<long int>; _Alloc = std::allocator<std::pair<const long int, Pessoa> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator = std::_Rb_tree_iterator<std::pair<const long int, Pessoa> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const long int, Pessoa>]
/usr/include/c++/4.7/bits/stl_map.h:522:7: note: candidate expects 1 argument, 2 provided
/usr/include/c++/4.7/bits/stl_map.h:574:7: note: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::iterator, const value_type&) [with _Key = long int; _Tp = Pessoa; _Compare = std::less<long int>; _Alloc = std::allocator<std::pair<const long int, Pessoa> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const long int, Pessoa> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const long int, Pessoa>]
/usr/include/c++/4.7/bits/stl_map.h:574:7: note: no known conversion for argument 1 from ‘long int’ to ‘std::map<long int, Pessoa>::iterator {aka std::_Rb_tree_iterator<std::pair<const long int, Pessoa> >}’
/usr/include/c++/4.7/bits/stl_map.h:598:9: note: template<class _InputIterator> void std::map::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = long int; _Tp = Pessoa; _Compare = std::less<long int>; _Alloc = std::allocator<std::pair<const long int, Pessoa> >]
/usr/include/c++/4.7/bits/stl_map.h:598:9: note: template argument deduction/substitution failed:
Pessoa.cpp:23:37: note: deduced conflicting types for parameter ‘_InputIterator’ (‘long int’ and ‘Pessoa’)


Em outros códigos tenho outros erros, mas a forma que declarei foi a mesma que fiz no exemplo pessoa. Não estou conseguindo entender como se usa o std::map, comecei a programar em C++ esse final de semana, em geral estou conseguindo me adaptar exceto pelo std. Qualquer ajuda é bem vinda.


: error: ‘Pessoa’ was not declared in this scope
: error: template argument 2 is invalid
: error: template argument 4 is invalid

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

Depois de tomar uma surra do std fizemos as pazes o erro estava na hora de inserir e nos demais códigos meus o erro estava na prototipagem, erro de iniciante mesmo.

Eu estava escrevendo


 

this->filhos.insert(p.getId(), p);

o correto é:

this->filhos.insert(std::pair<long, Pessoa>(p.getId(), p));

 

 

Arquivo cpp correto


#include <map>


#include "Pessoa.h"
using namespace std;


Pessoa::Pessoa() {
}


Pessoa::Pessoa(const Pessoa& orig) {
}


Pessoa::~Pessoa() {
}


bool Pessoa::addicionarFilhos(Pessoa p) {
    this->filhos.insert(std::pair<long, Pessoa>(p.getId(), p));
}


std::map<long, Pessoa> Pessoa::listarFilhos() {
    return this->filhos;
}

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.