I think there is no problem with the C++ header file, but I can't compile it.

Asked 2 years ago, Updated 2 years ago, 100 views

I tried to make a header file, but I can't compile it. No matter how much I look for it, it doesn't seem strange, so what's the problem?

datejin.h

#ifndef _DATEJIN_H_
#define _DATEJIN_H_

class datejin {

    int year, month, day;
public:
    int ldfm(int _year, int _month);
};

#endif

ldfm.cpp

#include "datejin.h"

int datejin::ldfm(int _year, int _month) {
    int _ldfm; //Last Day For Month

    switch (_month)
        {
            case 1:
                _ldfm = 31;
                break;
            case 2:
                if (_year % 4 == 0)
                {
                    if (_year % 100 == 0)
                    {
                        if (_year % 400 == 0)
                        {
                            _ldfm = 29;
                        }
                        else
                        {
                            _ldfm = 28;
                        }
                    }
                    else
                    {
                        _ldfm = 29;
                    }
                }
                else
                {
                    _ldfm = 28;
                }
                break;
            case 3:
                _ldfm = 31;
                break;
            case 4:
                _ldfm = 30;
                break;
            case 5:
                _ldfm = 31;
                break;
            case 6:
                _ldfm = 30;
                break;
            case 7:
                _ldfm = 31;
                break;
            case 8:
                _ldfm = 31;
                break;
            case 9:
                _ldfm = 30;
                break;
            case 10:
                _ldfm = 31;
                break;
            case 11:
                _ldfm = 30;
                break;
            case 12:
                _ldfm = 31;
                break;  
        }

    return _ldfm;
}

main.cpp

#include <iostream>
#include "datejin.h"

int main() {

    datejin A;

    std::cout << A.ldfm(2019,2) << std::endl;

    return 0;
}

If include is the main.cpp and datejin.cpp, the value is good. What's the problem?

c++ header-files

2022-09-22 20:14

1 Answers

If you compile directly with g++

g++ main.cppldfm.cpp

Try it like this.


2022-09-22 20:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.