Zador Pataki
Zador Pataki

Reputation: 11

I receive the error "expected an identifier" while creating a class with a constructor which takes parameters

Below you will find my code. First I present the header file then the corresponding cpp file. The error I receive is on the line of the constructor and states "expected an identifier" I have searched everywhere to find this error and I am stuck. It is also worth noting that I am not yet comfortable with classes in c++.

The header file:

#include <Eigen/Core>
#ifndef RIDGE_GD
#define RIDGE_GD


class RIDGE_GD
{
public:
    RIDGE_GD(double lambda, double eta, double max_iter);
    void fit(Eigen::MatrixXd X, Eigen::MatrixXd y);
    double error();

    Eigen::MatrixXd cost_grad();
    void gd_step();
    Eigen::MatrixXd X;
    Eigen::MatrixXd y;
    Eigen::MatrixXd w;

private:
    double m_lambda;
    double m_eta;
    double m_max_iter;

};


#endif

The corresponding cpp file:

    #include "RIDGE_GD.h"
    #include <Eigen/Core>
    
    RIDGE_GD::RIDGE_GD(double lambda, double eta, double max_iter)
    {
        m_lambda = lambda;
        m_eta = eta;
        m_max_iter = max_iter;


    }
    

    
    Eigen::MatrixXd RIDGE_GD::cost_grad(RIDGE_GD.X, RIDGE_GD.y, RIDGE_GD.w, RIDGE_GD.lambda) {};
    void RIDGE_GD::gd_step()
    {
    }
    
    
    void RIDGE_GD::fit(Eigen::MatrixXd X, Eigen::MatrixXd y)
    {}
    double RIDGE_GD::error()
    {}

Upvotes: 0

Views: 1258

Answers (2)

erenon
erenon

Reputation: 19118

The header guard is wrong, it changes the class name to nothing:

#ifndef RIDGE_GD
#define RIDGE_GD
^^ This line changes `class RIDGE_GD` to `class `, as RIDGE_GD is defined to be empty

Instead use a proper header guard:

#ifndef RIDGE_GD_HEADER_INCLUDED
#define RIDGE_GD_HEADER_INCLUDED

...

#endif

Upvotes: 0

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

Your include guard uses the same identifier as your class name. So all instances of RIDGE_GD are replaced by nothing. The compiler sees your class as

class {
public:
    (double lambda, double eta, double max_iter);
    // ...
};

which is why you get the error.

Use a different identifier for your include guard. (Or change the name of your class.)

Upvotes: 1

Related Questions