Toon posts:

C++ & pointers

Pagina: 1
Acties:

Verwijderd

Topicstarter
Het zal wel superstom zijn, maar ik kom er gewoon niet uit. (Ik ben c++ newbie). Ik heb de volgende headerfile:
code:
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
#ifndef VECTOR_H
#define VECTOR_H

/****************************************************************
 *
 *    vector.h
 *
 *    Author:  M.R.Nederlof
 *    Date:    August 1999
 *    Project: in3023 - 3D Computer Graphics and Virtual Reality
 *       Programming Assignment
 *
 ****************************************************************/

#include <glut.h>

class Vec3
{
  public:
    GLdouble x, y, z;

    Vec3 ();
    Vec3 (GLdouble _x, GLdouble _y, GLdouble _z);
    Vec3 (const Vec3& v);
    Vec3 (GLdouble *v);
    
    Vec3 &Set (GLdouble _x, GLdouble _y, GLdouble _z);
    Vec3 &Set (const Vec3& v);
    Vec3 &Set (GLdouble *v);
    
    Vec3&    Add   (const Vec3& a);
    Vec3&    Sub   (const Vec3& a);
    Vec3&    Mul   (const Vec3& a);
    Vec3&    Div   (const Vec3& a);
    Vec3&    Cross (const Vec3& a);
    Vec3&    Scale (GLdouble factor);
    Vec3&    Normalize ();
    GLdouble Length () const;
    GLdouble Dot (const Vec3& a) const;
    GLdouble Distance (const Vec3& v) const;
    void     Print (const char *text="") const;
    
    static Vec3 Add   (const Vec3& a, const Vec3& b);
    static Vec3 Sub   (const Vec3& a, const Vec3& b);
    static Vec3 Mul   (const Vec3& a, const Vec3& b);
    static Vec3 Div   (const Vec3& a, const Vec3& b);
    static Vec3 Cross (const Vec3& a, const Vec3& b);
    static Vec3 Scale (const Vec3& a, GLdouble factor);
    static Vec3 Normalize (const Vec3& a);
    
    Vec3& operator= (const Vec3& other) { return Set(other); }
};

class Vec4
{
  public:
    GLdouble x, y, z, w;

    Vec4 ();
    Vec4 (GLdouble _x, GLdouble _y, GLdouble _z, GLdouble _w);
    Vec4 (const Vec4& v);
    Vec4 (GLdouble *v);

    Vec4& Set (GLdouble _x, GLdouble _y, GLdouble _z, GLdouble _w);
    Vec4& Set (const Vec4& v);
    Vec4& Set (GLdouble *v);

    Vec4& Add   (const Vec4& a);
    Vec4& Sub   (const Vec4& a);
    Vec4& Mul   (const Vec4& a);
    Vec4& Div   (const Vec4& a);
    Vec4& Scale (GLdouble factor);
    Vec4& Normalize ();
    GLdouble    Length () const;
    GLdouble    Distance (const Vec4& v) const;
    void      Print (const char *text="") const;
    
    static Vec4 Add   (const Vec4& a, const Vec4& b);
    static Vec4 Sub   (const Vec4& a, const Vec4& b);
    static Vec4 Mul   (const Vec4& a, const Vec4& b);
    static Vec4 Div   (const Vec4& a, const Vec4& b);
    static Vec4 Cross (const Vec4& a, const Vec4& b);
    static Vec4 Scale (const Vec4& a, GLdouble factor);
    static Vec4 Normalize (const Vec4& a);

    Vec4& operator= (const Vec4& other) { return Set(other); }
};

class Matrix4
{
  public:
    GLdouble element[16];
  
    Matrix4 ();
    Matrix4 (const Matrix4& m);
    Matrix4 (const GLdouble *m);
    Matrix4 (const GLdouble **m);
    Matrix4 (GLdouble m11, GLdouble m21, GLdouble m31, GLdouble m41, 
         GLdouble m12, GLdouble m22, GLdouble m32, GLdouble m42, 
         GLdouble m13, GLdouble m23, GLdouble m33, GLdouble m43, 
         GLdouble m14, GLdouble m24, GLdouble m34, GLdouble m44);

    Matrix4& Set (const Matrix4& m);
    Matrix4& Set (const GLdouble *m);
    Matrix4& Set (const GLdouble **m);
    Matrix4& Set (GLdouble m11, GLdouble m21, GLdouble m31, GLdouble m41, 
            GLdouble m12, GLdouble m22, GLdouble m32, GLdouble m42, 
            GLdouble m13, GLdouble m23, GLdouble m33, GLdouble m43, 
            GLdouble m14, GLdouble m24, GLdouble m34, GLdouble m44);
    Matrix4& Identity ();

    Matrix4& Transpose ();
    Matrix4& Multiply (const Matrix4& m);
    Vec4     MultiplyByPoint (const Vec4& v);
    void     Print (const char *text="") const;
    
    static Matrix4 Transpose (const Matrix4& m);
    static Matrix4 Multiply (const Matrix4& m1, const Matrix4& m2);
    static Vec4    MultiplyByPoint (const Vec4& v, const Matrix4& m);

};

#endif    // VECTOR_H

Nu wil ik matrix1 met matrix2 vermenigvuldigen:
code:
1
2
3
4
Matrix4 *m1 = new Matrix4();
Matrix4 *m2 = new Matrix4();

m1->Multiply(m2);

Maar dat mag dus niet:
error C2664: 'class Matrix4 &__thiscall Matrix4::Multiply(const class Matrix4 &)' : cannot convert parameter 1 from 'class Matrix4 *' to 'const class Matrix4 &'
Reason: cannot convert from 'class Matrix4 *' to 'const class Matrix4'
No constructor could take the source type, or constructor overload resolution was ambiguous
Wat doe ik fout?

  • RdeTuinman
  • Registratie: Mei 2001
  • Laatst online: 16-08 07:25
Ik weet niet zeker hoe dat met dat 'const' zit, maar je geeft een pointer mee, en de functie wil een referentie. probeer eens:
code:
1
m1->Multiply(*m2);

Nou ben ik ook geen held met pointers hoor....

Verwijderd

Topicstarter
Voor mij ben je wel een held :P , ik heb nu geen errors meer. tnx!

  • RdeTuinman
  • Registratie: Mei 2001
  • Laatst online: 16-08 07:25
Op maandag 24 september 2001 10:46 schreef Tikker het volgende:
Voor mij ben je wel een held :P , ik heb nu geen errors meer. tnx!
Och, ja, nou, poeh. Ik ben blij dat ik weer iemand geholpen heb. Succes. :)

  • curry684
  • Registratie: Juni 2000
  • Laatst online: 04-09 14:38

curry684

left part of the evil twins

Op maandag 24 september 2001 10:51 schreef RdeTuinman het volgende:
Och, ja, nou, poeh. Ik ben blij dat ik weer iemand geholpen heb. Succes. :)
Uhmmmm no offense maar gezien het feit dat deze classes vrijwel 100% op const-references geschreven zijn zou ik als ik jou was stackallocated objects gebruiken ipv heap-allocatie, dit werkt bijv. een stuk strakker:
code:
1
2
3
4
Matrix4     m1;
Matrix4     m2;

m1->Multiply(m2);

Heb je ook geen geemmer met vrijgeven van geheugen, en een matrix en een vector zullen toch altijd lokaal of geaggregeerd zijn.

Professionele website nodig?


  • farlane
  • Registratie: Maart 2000
  • Laatst online: 20-09 16:35
m1->Multiply(m2);
Typo denk ik of niet.... ;)

Somniferous whisperings of scarlet fields. Sleep calling me and in my dreams i wander. My reality is abandoned (I traverse afar). Not a care if I never everwake.


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 21-09 12:16

.oisyn

Moderator Devschuur®

Demotivational Speaker

Op maandag 24 september 2001 11:44 schreef curry684 het volgende:
code:
1
m1->Multiply(m2);
FOEI curry! Bad dog! play dead! good dog! ;)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • curry684
  • Registratie: Juni 2000
  • Laatst online: 04-09 14:38

curry684

left part of the evil twins

Oeps nog niet wakker :)

Zal ie zelf ook wel achterkomen, dit geeft een duidelijke compiler error tenslotte O+

Ik dacht gewoon dat dit erbij stond:
code:
1
inline Matrix4* Matrix4::operator->() { return this; }

* curry684 fluit heel onschuldig... ;)

Professionele website nodig?


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 21-09 12:16

.oisyn

Moderator Devschuur®

Demotivational Speaker

wheehehehe LOL, das ook een manier om compiler errors eruit te krijgen :D

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.

Pagina: 1