Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implementation of a class into a header file and .cpp file

I’ve been learning c++ and trying to implement a class i made in a solution to a header file and source file for future use.in the header file i have implemented the following definitions and methods

#ifndef CVECTOR_H
#define CVECTOR_H

    class cVector {
    public:
        float Xpos;
        float Ypos;
        float Zpos;
        cVector(float x, float y, float z);

        void Print();

        float Length();

        cVector Negation();

        cVector Normalisation();
    };

#endif

and in the source file (.cpp) i have tried to define

    #include<iostream>
    #include<math.h>
    #include "cVector.h"
    #include <float.h>




    int main()
    {
        cVector(float x, float y, float z) { // Constructor with parameters
            Xpos = x;
            Ypos = y;
            Zpos = z;
        };
    }

Yet i get the error that float x is not a valid type name, what is the error?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i wanted the implementation to define the cVector input to the variables Xpos ,Ypos , Zpos

>Solution :

This would be the correct syntax. This needs to be placed outside of main():

cVector::cVector(float x, float y, float z) { // Constructor with parameters
  Xpos = x;
  Ypos = y;
  Zpos = z;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading