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

C++ Discussion :

Conversion impossible objet classe fille en classe mere


Sujet :

C++

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Architecte matériel
    Inscrit en
    Juin 2015
    Messages
    31
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Architecte matériel

    Informations forums :
    Inscription : Juin 2015
    Messages : 31
    Par défaut Conversion impossible objet classe fille en classe mere
    Bonjour,
    Je dispose d'une fonction qui prend comme paramètre un objet de classe mere, lorsque j'essaye d'appeler cette fonction avec comme parametre un objet de classe fille le compilateur me jette, je ne comprend pas pourquoi, un objet de classe filles est bien considérer comme un objet de la classe mère "particulier" pourtant

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2013
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2013
    Messages : 25
    Par défaut
    En effet. A priori, pas de raison que ta classe fille soit refusée.

    Le problème vient peut être d'une erreur de syntaxe, d'une erreur dans la déclaration de la classe fille, d'un fichier include manquant qui ne permet pas au compilateur de savoir que cette classe fille hérite de la classe mère susnommée, etc ...

    Peut être qu'avec un extrait du code, une hypothèse se confirmera. Le message d'erreur exact pourrait être utile également.

  3. #3
    Membre averti
    Homme Profil pro
    Architecte matériel
    Inscrit en
    Juin 2015
    Messages
    31
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Architecte matériel

    Informations forums :
    Inscription : Juin 2015
    Messages : 31
    Par défaut
    impossible de convertir le paramètre 3 de 'std::vector<_Ty>' en 'std::vector<_Ty> &'
    1> with
    1> [
    1> _Ty=Point3D_X_highter
    1> ]
    1> and
    1> [
    1> _Ty=Point3D
    1> ]
    1> main.cpp(36) : voir la référence à l'instanciation de la fonction modèle 'int execution<Point3D_X_highter>(std::map<_Kty,_Ty> &,std::vector<Point3D_X_highter> &,std::vector<Triangle> &,unsigned int)' en cours de compilation
    1> with
    1> [
    1> _Kty=Point3D_X_highter,
    1> _Ty=int
    1> ]
    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    #ifndef GEO
    #define GEO
     
    #include <cstdlib>
    #include <cstdio>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <stdint.h>
     
    //[comment]
    // Implementation of a generic vector class - it will be used to deal with 3D points, vectors and normals.
    // The class is implemented as a template. While it may complicate the code a bit, it gives us
    // the flexibility later, to specialize the type of the coordinates into anything we want.
    // For example: Vec3f if we want the coordinates to be floats or Vec3i if we want the coordinates to be integers.
    //
    // Vec3 is a standard/common way of naming vectors, points, etc. The OpenEXR and Autodesk libraries
    // use this convention for instance.
    //[/comment]
    template<typename T>
    class Vec3
    {
    public:
    	Vec3() : x(T(0)), y(T(0)), z(T(0)) {}
    	Vec3(T xx) : x(xx), y(xx), z(xx) {}
    	Vec3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}
    	Vec3 operator + (const Vec3 &v) const
    	{ return Vec3(x + v.x, y + v.y, z + v.z); }
    	Vec3 operator - (const Vec3 &v) const
    	{ return Vec3(x - v.x, y - v.y, z - v.z); }
    	Vec3 operator - () const
    	{ return Vec3(-x, -y, -z); }
    	Vec3 operator * (const T &r) const
    	{ return Vec3(x * r, y * r, z * r); }
    	Vec3 operator * (const Vec3 &v) const
    	{ return Vec3(x * v.x, y * v.y, z * v.z); }
    	T dotProduct(const Vec3<T> &v) const
    	{ return x * v.x + y * v.y + z * v.z; }
    	Vec3& operator /= (const T &r)
    	{ x /= r, y /= r, z /= r; return *this; }
    	Vec3& operator *= (const T &r)
    	{ x *= r, y *= r, z *= r; return *this; }
    	Vec3 crossProduct(const Vec3<T> &v) const
    	{ return Vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); }
    	T norm() const
    	{ return x * x + y * y + z * z; }
    	T length() const
    	{ return sqrt(norm()); }
    // fonction non implémentée pour la classe mere du coup
    	/*bool operator < (const Vec3<T> &v) const
    	{
    	if (x != v.x)
    	return (x < v.x);
    	if (y != v.y)
    	return (y < v.y);
    	return (z < v.z);
    	}*/
     
    	//[comment]
    	// The next two operators are sometimes called access operators or
    	// accessors. The Vec coordinates can be accessed that way v[0], v[1], v[2],
    	// rather than using the more traditional form v.x, v.y, v.z. This useful
    	// when vectors are used in loops: the coordinates can be accessed with the
    	// loop index (e.g. v[i]).
    	//[/comment]
    	const T& operator [] (uint8_t i) const { return (&x)[i]; }
    	T& operator [] (uint8_t i) { return (&x)[i]; }
    	Vec3& normalize()
    	{
    		T n = norm();
    		if (n > 0) {
    			T factor = 1 / sqrt(n);
    			x *= factor, y *= factor, z *= factor;
    		}
    		return *this;
    	}
     
    	friend Vec3 operator * (const T &r, const Vec3 &v)
    	{ return Vec3<T>(v.x * r, v.y * r, v.z * r); }
    	friend Vec3 operator / (const T &r, const Vec3 &v)
    	{ return Vec3<T>(r / v.x, r / v.y, r / v.z); }
     
    	friend std::ostream& operator << (std::ostream &s, const Vec3<T> &v)
    	{
    		return s << '[' << v.x << ' ' << v.y << ' ' << v.z << ']';
    	}
     
     
    	T x, y, z;
    };
    template<typename T>
    T max3Values(T const& a, T const& b, T const& c){
    	return std::max(a,std::max(b,c));
    }
    template<typename T>
    T min3Values(T const& a, T const& b, T const& c){
    	return std::min(a,std::min(b,c));
    }
    //[comment]
    // Now you can specialize the class. We are just showing two examples here. In your code
    // you can declare a vector either that way: Vec3<float> a, or that way: Vec3f a
    //[/comment]
    typedef Vec3<float> Vec3f;
    typedef Vec3<float> Point3D;
    typedef Vec3<float> Normal;
    typedef Vec3<int> Vec3i;
    typedef Vec3<int> indPointsTriangle;
     
     
    class Point3D_X_highter : public Point3D
    {
    	template<typename T>
    	bool operator < (const Vec3<T> &v) const
    	{
    		if (x != v.x)
    			return (x < v.x);
    		if (y != v.y)
    			return (y < v.y);
    		return (z < v.z);
    	}
    };
     
     
    class Point3D_Y_highter: public Point3D
    {
    	template<typename T>
    	bool operator < (const Vec3<T> &v) const
    	{
    		if (y != v.y)
    			return (y < v.y);
    		if (x != v.x)
    			return (x < v.x);
    		return (z < v.z);
    	}
    };
     
    class Point3D_Z_highter: public Point3D
    {
    	template<typename T>
    	bool operator < (const Vec3<T> &v) const
    	{
    		if (z != v.z)
    			return (z < v.z);
    		if (x != v.x)
    			return (x < v.x);
    		return (y < v.y);
    	}
    };
    #endif

  4. #4
    Membre Expert
    Avatar de white_tentacle
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    1 505
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 1 505
    Par défaut
    Un std::vector<B> n’hérite jamais d’un std::vector<A>, même si B hérite de A.

    Sauf si j’ai lu trop vite, c’est ça ton problème (et donc parfaitement normal que ça ne compile pas).

  5. #5
    Expert éminent

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par défaut
    Un compilateur qui me dit "impossible de convertir 'truc' en 'truc &'", pour moi, c'est surtout une tentative de prendre une temporaire en référence modifiante.
    Il n'y a que deux solutions: prendre par copie (ou faire une copie avant de prendre la référence), et prendre en référence constante.

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    1 415
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2007
    Messages : 1 415
    Par défaut
    Il semble que les deux erreurs ont été commises en même temps.

  7. #7
    Membre averti
    Homme Profil pro
    Architecte matériel
    Inscrit en
    Juin 2015
    Messages
    31
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Architecte matériel

    Informations forums :
    Inscription : Juin 2015
    Messages : 31
    Par défaut
    après rectification sur conseil de leternel, l'erreur devient
    impossible de convertir le paramètre 2 de 'std::map<_Kty,_Ty>' en 'std::map<_Kty,_Ty>'
    1> with
    1> [
    1> _Kty=Point3D_X_highter,
    1> _Ty=int
    1> ]
    1> and
    1> [
    1> _Kty=Point3D,
    1> _Ty=int
    1> ]
    1> Aucun opérateur de conversion définie par l'utilisateur disponible qui puisse effectuer cette conversion, ou l'opérateur ne peut pas être appelé
    Donc en effet un vector<A> ou map<A> ne peut pas hérité d'un vector<B> ou une map<B>

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Caster classe mère et classe fille
    Par zsoufianz dans le forum Général Java
    Réponses: 1
    Dernier message: 02/12/2014, 03h36
  2. Conversion d'une classe mère en classe fille
    Par Aleph69 dans le forum C++
    Réponses: 7
    Dernier message: 07/05/2014, 10h39
  3. Appel méthode classe fille, dont classe mère virtuelle
    Par Metalman dans le forum Débuter
    Réponses: 6
    Dernier message: 07/06/2013, 11h51
  4. Réponses: 6
    Dernier message: 22/07/2010, 15h17
  5. caster une classe mère en classe fille
    Par franchouze dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 01/10/2009, 11h56

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