468,167 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,167 developers. It's quick & easy.

Construct a Vector by subtracting 2 verticies

2 2Bits
For my assignment, I need to construct a function that takes 2 vertices(any vertex), subtract them and return a vector
The vertices are defined in the Vertex class and the vectors in Vector3D class

the task
Screenshot_216.png
Screenshot_217.png

I put the declaration of the function in the Vertex header (and her body in the Vertex class) because it needs to be called from the vertices

Expand|Select|Wrap|Line Numbers
  1.   // The decleration in the Vertex header
  2.  
  3.         Vector3D VecSub(Vertex const &other);
  4.  
  5.  
  6.         //The body in the Vertex Class
  7.  
  8.         Vector3D Vertex::VecSub(Vertex const &other) 
  9.  
  10.         {
  11.         Vertex a;
  12.         Vertex b;
  13.         Vertex sub = a - b;
  14.         Vector3D temp(sub.GetX(), sub.GetY(), sub.GetZ());;
  15.         return temp;
  16.         }
  17.  
  18.  
  19.         //The use of the function in the model class
  20.  
  21.         Vector3D vectorA = vertex1.VecSub(vertex0);
  22.         Vector3D vectorB = vertex2.VecSub(vertex0);
  23.  
  24.         Vector3D EyeVector = 
  25.         vertex0.VecSub(camera.GetPos());



When I debug the program I am getting this errors
Screenshot_215.jpg


The declaration of 'Vertex'

Expand|Select|Wrap|Line Numbers
  1. Vertex(float x, float y, float z, float w);

And the declaration of 'Vertex3D

Expand|Select|Wrap|Line Numbers
  1. Vector3D(float x, float y, float z);
`
Attached Images
File Type: png Screenshot_216.png (3.6 KB, 14 views)
File Type: png Screenshot_217.png (1.9 KB, 11 views)
File Type: jpg Screenshot_215.jpg (83.5 KB, 14 views)
Feb 8 '21 #1

✓ answered by Banfa

OK does Vector3D.h include Vertex.h (or stdafx.h)? Circular inclusion tends to lead to strange errors.

This compiles fine if comment out the include stdafx.h and create my own Vector3D,h like so
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2.  
  3. class Vector3D
  4. {
  5.   public:
  6.     Vector3D() {}
  7.     virtual ~Vector3D() {}
  8. };
  9.  

3 1730
Banfa
9,033 Expert Mod 8TB
It looks like you have not shown us the actual code causing the error, 1 line from a header file isn't really enough, I assume that line 3 of the posted code is actually line 31 of Vertex.h.

What the error is telling you, in a rather obtuse way, is that it doesn't know what Vector3D is at this point. An override specifier is some combination of the keywords override or final that should appear at the end of the function definition.

It appears likely that you have used the Vector3D type without including the header file that defines it.
Feb 9 '21 #2
Eldyr
2 2Bits
I have included the Vector3D.h to the Vertex header

The Vertex header
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #include "Vector3D.h"
  3. #include "stdafx.h"
  4.  
  5. class Vertex
  6. {
  7. public:
  8.     Vertex();
  9.     Vertex(float x, float y, float z, float w);
  10.  
  11.     //Copy constructor
  12.     Vertex(const Vertex& v);
  13.  
  14.     //Destructor
  15.     ~Vertex();
  16.  
  17.     //Accessors and mutators
  18.     float GetX() const;
  19.     int GetIntX() const;
  20.     void SetX(const float x);
  21.     float GetY() const;
  22.     int GetIntY() const;
  23.     void SetY(const float y);
  24.     float GetZ() const;
  25.     void SetZ(const float z);
  26.     float GetW() const;
  27.     void SetW(const float w);
  28.  
  29.     void Dehomogenize();
  30.  
  31.  
  32.         Vector3D VecSub(Vertex const &other);
  33.  
  34.     //Assignment operator
  35.     Vertex& operator= (const Vertex& rhs);
  36.  
  37.     bool operator== (const Vertex& rhs) const;
  38.  
  39.     const Vertex operator+ (const Vertex& rhs) const;
  40.  
  41.     const Vertex operator- (const Vertex& rhs) const;
  42.  
  43.  
  44. private:
  45.     float _x;
  46.     float _y;
  47.     float _z;
  48.     float _w;
  49.  
  50. };
Feb 9 '21 #3
Banfa
9,033 Expert Mod 8TB
OK does Vector3D.h include Vertex.h (or stdafx.h)? Circular inclusion tends to lead to strange errors.

This compiles fine if comment out the include stdafx.h and create my own Vector3D,h like so
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2.  
  3. class Vector3D
  4. {
  5.   public:
  6.     Vector3D() {}
  7.     virtual ~Vector3D() {}
  8. };
  9.  
Feb 10 '21 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Jessica | last post: by
6 posts views Thread by Lasse Skyum | last post: by
2 posts views Thread by The Directive | last post: by
7 posts views Thread by ES Kim | last post: by
15 posts views Thread by Alex Vinokur | last post: by
6 posts views Thread by Michael | last post: by
42 posts views Thread by barcaroller | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.