473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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, 35 views)
File Type: png Screenshot_217.png (1.9 KB, 34 views)
File Type: jpg Screenshot_215.jpg (83.5 KB, 35 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 2216
Banfa
9,065 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,065 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

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

Similar topics

4
by: Jessica | last post by:
Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the...
6
by: Lasse Skyum | last post by:
I'm currently learning STL and I hate not knowing what is gooing on "inside" STL... not because I really _need_ to know it to develop my game project, but that's just my nature... like most of you...
2
by: The Directive | last post by:
I'm confused as to why C++ does not allow to create a vector of references (even though a vector of pointers can be created). I read some old threads about it but I still don't fully understand it....
7
by: ES Kim | last post by:
It is well known that vector::swap can be used to effectively free the allocated memory. ie, vector<int> v; vector<int>().swap(v); Someone suggested another way using a placement-new. ...
15
by: Alex Vinokur | last post by:
I am looking for any custom allocator sample code for std::vector. Thanks. -- Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
7
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). ...
6
by: Michael | last post by:
Hi, What's wrong with the following code? Please help. Thanks, Michael #include <iostream> #include <vector> using namespace std;
4
by: Rakesh Kumar | last post by:
Hi All - In a project of mine - I was trying to scale down the actual issue to the following piece of code. I need to allocate an array of strings and reserve the individual string to a particular...
0
by: tech | last post by:
Hi, I have developed a little class to help implement dynamic menus in our application which has a UI. This allows our engine to send a MenuItem object to the UI which is an observer of the...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.