@dmitriyprog

Как скомпилировать 3 файла в с++?

Проблема линковки
Файл date.hpp
#include <string>
#ifndef __CCLASS_H__
#define __CCLASS_H__
using namespace std;

enum dayOfWeek {MON, TUE, WEN, THU, FRI, SAT, SUN};


/**
* The class  describes the date represents as day,month and year
* Each Date can be additioned or subsructed with a integer number
*
*/
class Date
{
    private:
        int year;
        int month;
        int day;

   public:

      // FRIEND_TEST(Date, getMonth);
       Date(int day, int month, int year);

       static const int MaxDayInMonth[13];
       /**
       * Checks if the set year leap is
         return true if thee year leap is.
       */
       inline bool Leap(int year);
       /**
       * Gets year value;
       */
       int getYear() /*override */;
        /**
        * Gets month as a number.
        *
        * return the month as a nuber.
        */
       int getMonth() /*override */;
       /**
        Gets day as a number
       */
       int getDay()  /*override */;
       /**
       * Adds the number to current date;
       *
       * return new date
       */
       Date operator + (int k);
       /**
       * Substracts the number from current date;
       *
       * return new date
       */
       Date operator - (int k);
       /**
       * Represents the date as a string
       *
       * return string in format yyyy-mm-dd
       */
       string toString() /*override */;
       /**
        * Prints the date on console.
       */

       void printDate() /*override */;

       dayOfWeek day_of_week();

};

#endif

Файл date.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "date.h"
#include <gtest/gtest.h>

using namespace std;

const int Date::MaxDayInMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};


Date::Date(int day, int month, int year)
{
 try
    {
     if (month < 1 || month >12)
         throw 'm';
     if (year < 1970 || year > 2019)
         throw 'y';
     this->day = day;
     this->month = month;
     this->year = year;
    }
    catch(char c)
    {
      if (c == 'm')
        cout << "The month is wrong. Try again, please." <<endl;
       if (c == 'y')
        cout << "The year is wrong. Try again, please." <<endl;
    }

}
inline bool Date::Leap(int year)
{
    return year % 4 ==  0 && (year % 100 != 0 || year % 400 == 0);
}
 int Date::getYear() /*override*/
{
    return year;
}
int Date::getMonth() /*override*/
{
    return month;
}

int Date::getDay() /*override*/
{
    return day;
}

Date Date::operator + (int k)
{
    int maxDayInMonth = MaxDayInMonth[month];
    int newDays = day +  k;
    if (month == 2)
    {
        maxDayInMonth = (Leap(year) ? maxDayInMonth + 1 : maxDayInMonth);
    }
    if ((day +  k) >  maxDayInMonth)
    {
        newDays = (day +  k ) % maxDayInMonth ;
        month ++;
        if (month > 12)
        {
          month = 1;
          year++;
        }
    }

    return Date(newDays,this->month,this->year);
}

Date Date::operator - (int k)
{
    int newDays = (this->day - k);
    if (newDays < 1)
    {
      month--;
      k = k - day;
      month = (month < 1 ? 12 : month);
      if (month == 12)
      {
        year--;
      }
      int maxDayInMonth = MaxDayInMonth[month];
      if (month == 2)
      {
        maxDayInMonth = (Leap(year) ? maxDayInMonth + 1 : maxDayInMonth);
      }
      day = maxDayInMonth - k;
    }
    return Date(this->day,this->month,this->year);
}

string Date::toString() /*override*/
{
  std::stringstream output;
  output << year << "-" << month << "-" << day;
  return output.str();
}

void Date::printDate() /*override*/
{
  cout << this->toString() << endl;
}



TEST(DateTest, firstTest)
{
    Date d(12,12,2005);
    ASSERT_EQ(12, d.getMonth());
  //  ASSERT_EQ("2005-12-12", d.toString());
}



TEST(DateTest, stringTest)
{
    Date d(12,12,2005);
    ASSERT_EQ("2005-12-12", d.toString());
    Date d1 = d + 5;
    ASSERT_EQ("2005-12-17", d1.toString());
    Date d2 = d - 1;
    ASSERT_EQ("2005-12-11", d2.toString());
    Date d

}

Файл main.cpp
<code lang="cpp">
int main(int, char **)
{
    Date date(1,12,2017);
    Date date2(26,1,2005);
    cout << date.getMonth() << endl;
    Date d = date - 6;
    d.printDate();
    return 0;
}


</code>
  • Вопрос задан
  • 125 просмотров
Пригласить эксперта
Ответы на вопрос 1
BacCM
@BacCM
C++ почти с рождения
Используй какой-либо сборщик проектов. Если винда, то проект в VS
Если линукс, то проще всего наверно Qt Creator поставить.

Можно просто перечислить в том чем компилируешь файлы .cpp

Хотя самое правильное, это изучить CMake или еще что-то подобное.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы