IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

[unique_ptr] Erreur en compilation.


Sujet :

Langage C++

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité
    Invité(e)
    Par défaut [unique_ptr] Erreur en compilation.
    Salut,

    J'essaye de retourner un pointeur sur un objet issus d'un std::unique_ptr :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Entity* CellMap::getEntityInside (unsigned int index) {
         if (index >= 0 && index < entityInside.size()) {
             return entityInside[index].get();
         }
         return nullptr;
    }

    mais le compilateur (Clang 3.5) me sort cette erreur : (Hors que je n'alloue aucun objet)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/unique_ptr.h|765|error: allocating an object of abstract class type 'odfaeg::graphic::Entity'|

  2. #2
    Membre émérite

    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 403
    Par défaut
    Ta classe Entity est abstraite et ne peut pas être allouée. Soit ce n'est pas ce code qui provoque cette erreur, soit tu utilises une allocation "cachée" (par exemple si entityInside est une std::map ? Mais dans ce cas, le if serait étrange)

    HS : A mon sens, il serait mieux d'utiliser un assert plutôt qu'un if ici, ce qui permet de virer la contrainte "retourne nullptr si l'index n'est pas valide" et donc retourner une référence sur unique_ptr plutôt qu'un pointeur nu
    HS2 : c'est inutile de tester si un unisgned est positif

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    unique_ptr<Entity>& CellMap::getEntityInside (unsigned int index) {
        assert (index < entityInside.size());
        return entityInside[index];
    }
     
    const unique_ptr<Entity>& CellMap::getEntityInside (unsigned int index) const {
        assert (index < entityInside.size());
        return entityInside[index];
    }

  3. #3
    Invité
    Invité(e)
    Par défaut
    Non entityInside est un vector, bref, je vais essayer de faire comme tu m'as dis.

  4. #4
    Membre émérite

    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 403
    Par défaut
    Donc l'erreur n'est pas dans le code donné. Montre la pile d'erreurs complète

  5. #5
    Invité
    Invité(e)
    Par défaut
    Ok, la voici.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
    ||=== Build: Debug in ODFAEG (compiler: GNU GCC Compiler) ===|
    ||warning: optimization flag '-fexpensive-optimizations' is not supported|
    ||warning: argument unused during compilation: '-fexpensive-optimizations'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Math/../../../include/odfaeg/Core/fastDelegate.h|170|warning: 'RefVal' defined as a struct template here but previously declared as a class template [-Wmismatched-tags]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Math/../../../include/odfaeg/Core/fastDelegate.h|70|note: did you mean struct here?|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Math/../../../include/odfaeg/Core/resourceManager.h|528|warning: unused variable 'id' [-Wunused-variable]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/boundingVolume.h|60|warning: 'odfaeg::physic::BoundingVolume::intersects' hides overloaded virtual functions [-Woverloaded-virtual]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/boundingVolume.h|36|note: hidden overloaded virtual function 'odfaeg::physic::BaseInterface::intersects' declared here: type mismatch at 1st parameter ('odfaeg::physic::BoundingBox &' vs 'odfaeg::physic::BaseInterface &')|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/boundingVolume.h|37|note: hidden overloaded virtual function 'odfaeg::physic::BaseInterface::intersects' declared here: type mismatch at 1st parameter ('odfaeg::physic::BoundingSphere &' vs 'odfaeg::physic::BaseInterface &')|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/boundingVolume.h|38|note: hidden overloaded virtual function 'odfaeg::physic::BaseInterface::intersects' declared here: type mismatch at 1st parameter ('odfaeg::physic::BoundingEllipsoid &' vs 'odfaeg::physic::BaseInterface &')|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/boundingVolume.h|39|note: hidden overloaded virtual function 'odfaeg::physic::BaseInterface::intersects' declared here: type mismatch at 1st parameter ('odfaeg::physic::OrientedBoundingBox &' vs 'odfaeg::physic::BaseInterface &')|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/boundingVolume.h|40|note: hidden overloaded virtual function 'odfaeg::physic::BaseInterface::intersects' declared here: type mismatch at 1st parameter ('odfaeg::physic::BoundingPolyhedron &' vs 'odfaeg::physic::BaseInterface &')|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|187|warning: '&&' within '||' [-Wlogical-op-parentheses]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|187|note: place parentheses around the '&&' expression to silence this warning|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|187|warning: '&&' within '||' [-Wlogical-op-parentheses]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|187|note: place parentheses around the '&&' expression to silence this warning|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|292|warning: '&&' within '||' [-Wlogical-op-parentheses]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|292|note: place parentheses around the '&&' expression to silence this warning|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|292|warning: '&&' within '||' [-Wlogical-op-parentheses]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|292|note: place parentheses around the '&&' expression to silence this warning|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|416|warning: '&&' within '||' [-Wlogical-op-parentheses]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|416|note: place parentheses around the '&&' expression to silence this warning|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|416|warning: '&&' within '||' [-Wlogical-op-parentheses]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Physics/../Math/computer.h|416|note: place parentheses around the '&&' expression to silence this warning|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/../Math/../../../include/odfaeg/Core/stateParameter.h|32|warning: field 'name' will be initialized after field 'value' [-Wreorder]|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/cellMap.cpp|62|warning: comparison of unsigned expression >= 0 is always true [-Wtautological-compare]|
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/unique_ptr.h|765|error: allocating an object of abstract class type 'odfaeg::graphic::Entity'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/cellMap.cpp:19:41: note||in instantiation of function template specialization 'std::make_unique<odfaeg::graphic::Entity, odfaeg::graphic::Entity *>' requested here|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/entity.h|120|note: unimplemented pure virtual method 'operator==' in 'Entity'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/entity.h|188|note: unimplemented pure virtual method 'isAnimated' in 'Entity'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/entity.h|195|note: unimplemented pure virtual method 'isModel' in 'Entity'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/entity.h|201|note: unimplemented pure virtual method 'selectable' in 'Entity'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/entity.h|207|note: unimplemented pure virtual method 'isLight' in 'Entity'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/entity.h|213|note: unimplemented pure virtual method 'isShadow' in 'Entity'|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/../../../include/odfaeg/Graphics/entity.h|219|note: unimplemented pure virtual method 'isLeaf' in 'Entity'|
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_construct.h|75|error: call to deleted constructor of 'std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> >'|
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_uninitialized.h:75:8: note||in instantiation of function template specialization 'std::_Construct<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> >, const std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > &>' requested here|
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_uninitialized.h:125:2: note||in instantiation of function template specialization 'std::__uninitialized_copy<false>::__uninit_copy<__gnu_cxx::__normal_iterator<const std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > *, std::vector<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> >, std::allocator<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > > > >, std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > *>' requested here|
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_uninitialized.h:278:19: note||in instantiation of function template specialization 'std::uninitialized_copy<__gnu_cxx::__normal_iterator<const std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > *, std::vector<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> >, std::allocator<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > > > >, std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > *>' requested here|
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_vector.h:322:9: note||in instantiation of function template specialization 'std::__uninitialized_copy_a<__gnu_cxx::__normal_iterator<const std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > *, std::vector<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> >, std::allocator<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > > > >, std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > *, std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > >' requested here|
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/cellMap.cpp:33:20: note||in instantiation of member function 'std::vector<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> >, std::allocator<std::unique_ptr<odfaeg::graphic::Entity, std::default_delete<odfaeg::graphic::Entity> > > >::vector' requested here|
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/unique_ptr.h|356|note: 'unique_ptr' has been explicitly marked deleted here|
    ||=== Build failed: 2 error(s), 19 warning(s) (0 minute(s), 9 second(s)) ===|
    Le problème avec les pointeurs intelligent c'est que ça me met la ligne d'erreur dans le fichier std::unique_ptr et non pas dans mon code...

  6. #6
    Membre émérite

    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 403
    Par défaut
    Faut pas exagérer. La STL n'est pas parfaite, les pré-conditions ne sont pas forcement vérifiées et les messages d'erreur ne sont pas toujours explicite (cela devrait s'améliorer avec les concepts), mais il est difficile de critiquer les devs de la STL alors que la plupart des développeurs font pareil.

    En plus, la pile d'erreur permet souvent de retrouver la fonction dans la code appelant qui pose problème. Dans ton cas, c'est un appel de make_unique sur Entity, dans cellMap.cpp à la ligne 19 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/cellMap.cpp:19:41: note||in instantiation of function template specialization 'std::make_unique<odfaeg::graphic::Entity, odfaeg::graphic::Entity *>' requested here|
    Sinon, les warning ne sont pas décoratifs, il faut les régler. Tu en particulier des problèmes de masquage de fonction, utilises override.

    HS : tes fonctions qui posent problème dans Entity (isLight, isShadow, etc) me fait penser à un problème de conception. Ta classe Entity semble devoir connaitre le type réel des classes enfants manipulées, ce qui ne va pas

  7. #7
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 150
    Billets dans le blog
    4
    Par défaut
    Le problème c'est surtout que tu remets toujours promptement le reste du monde en question avant toi-même. Hors ton historique prouve que l'inverse est vrai dans 99.99% des cas..
    En l'occurence l'erreur est plutôt bien visible /home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/cellMap.cpp:19:41: note||in instantiation of function template specialization 'std::make_unique<odfaeg::graphic::Entity, odfaeg::graphic::Entity *>' requested here|, encore faut-il lire la sortie.
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

Discussions similaires

  1. Erreur de compilation après modification du Uses
    Par DevelOpeR13 dans le forum Langage
    Réponses: 5
    Dernier message: 30/10/2007, 14h23
  2. Réponses: 2
    Dernier message: 23/09/2003, 14h32
  3. Réponses: 10
    Dernier message: 22/09/2003, 21h58
  4. Réponses: 4
    Dernier message: 27/08/2003, 21h34
  5. Réponses: 2
    Dernier message: 04/03/2003, 23h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo