Home > OS >  Implementation of a class into a header file and .cpp file
Implementation of a class into a header file and .cpp file

Time:03-27

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?

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

CodePudding user response:

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;
}
  • Related