0

Is it possible to call cpp file into main, without using header? I tried in VS 2019 something like that:

main.cpp

#include<iostream>

int main()
{
    Date birthday{3,2,2};
    birthday.getDay();
    std::cout << "User, input the day";
    int u_day = 0;
    std::cin >> u_day;
    birthday.print(u_day);
    return 0;

}

another.cpp

#include<iostream>

class Date
{
private:
    int m_year;
    int m_month;
    int m_day;
public:
    Date() = default;

    Date(int year, int month, int day) {
        setDate(year, month, day);
    }
    void setDate(int year, int month, int day) {
        m_year = year;
        m_month = month;
        m_day = day;
    }
    int getYear() { return m_year; }
    int getMonth() { return m_month; }
    int getDay() { return m_day; }

    void print(int day) {
        m_day = day;

        std::cout << m_day;
    }
};

Now there are a lot of C2056 errors, When I add Date::Date birthday{3,2,2}; in the main.cpp I get E0276, E0065, C2653,C2065,C2146

brenqP
  • 35
  • 5
  • error codes are not universal. Are you using msvc? In any case you better include the compiler error message in the question. And how do you compile the code? – 463035818_is_not_an_ai Jun 09 '21 at 09:31
  • 1
    `Is it possible to call cpp file into main?` Possible, but never encouraged : https://stackoverflow.com/questions/19547091/including-cpp-files – silverfox Jun 09 '21 at 09:32
  • 1
    Code that uses class object must have access to the class definition. Also, when creating a local object, it must have access to the declaration of constructor and destructor of that class. "Access" here means that the declaration/definition was already in the code before the use (`#include` simply copies and pastes content of a file into another file). It's not negotiable, and that's why we normally need header files. There are also modules in C++20, but I never used that feature and I don't really know if it replaces header files or how does it work. – Yksisarvinen Jun 09 '21 at 09:43

0 Answers0