C++ 编译C++;在Windows上(Mingw/Cygwin)

C++ 编译C++;在Windows上(Mingw/Cygwin),c++,windows,c++11,C++,Windows,C++11,我正试图为一个学校项目编译我的第一个c++11文件,但我在cygwin和mingw环境中都遇到了一些错误,可能我缺少了需要安装的部分,但这应该是一件非常简单的事情。唉,我对C++编译一无所知,所以我不知道从哪里开始查找。 这是我得到的输出: g++ -g -O2 -Wall -W -pedantic-errors -Wmissing-braces -Wparentheses -Wold-style-cast -std=c++11 -c -o date.o date.cc In file

我正试图为一个学校项目编译我的第一个c++11文件,但我在cygwin和mingw环境中都遇到了一些错误,可能我缺少了需要安装的部分,但这应该是一件非常简单的事情。唉,我对C++编译一无所知,所以我不知道从哪里开始查找。 这是我得到的输出:

g++  -g -O2 -Wall -W -pedantic-errors -Wmissing-braces -Wparentheses -Wold-style-cast  -std=c++11    -c -o date.o date.cc
In file included from date.cc:6:0:
date.h: In constructor ‘Date::Date(int, int, int)’:
date.h:20:6: warning: ‘Date::day’ will be initialized after [-Wreorder]
  int day;   // the day (1-..)
      ^
date.h:19:6: warning:   ‘int Date::month’ [-Wreorder]
  int month; // the month (1-12)
      ^
date.cc:24:1: warning:   when initialized here [-Wreorder]
 Date::Date(int y, int m, int d) : year(y), day(d), month(m) {}
 ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_pair.h:59:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_algobase.h:64,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/char_traits.h:39,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from date.cc:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h: In instantiation of ‘void std::swap(_Tp&, _Tp&) [with _Tp = const Date]’:
date.cc:77:13:   required from here
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:176:11: error: no match for ‘operator=’ (operand types are ‘const Date’ and ‘std::remove_reference<const                                              Date&>::type {aka const Date}’)
       __a = _GLIBCXX_MOVE(__b);
           ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:176:11: note: candidates are:
In file included from date.cc:6:0:
date.h:9:7: note: Date& Date::operator=(const Date&) <near match>
 class Date {
       ^
date.h:9:7: note:   no known conversion for implicit ‘this’ parameter from ‘const Date*’ to ‘Date*’
date.h:9:7: note: Date& Date::operator=(Date&&)
date.h:9:7: note:   no known conversion for argument 1 from ‘std::remove_reference<const Date&>::type {aka const Date}’ to ‘Date&&’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_pair.h:59:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_algobase.h:64,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/char_traits.h:39,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from date.cc:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:177:11: error: no match for ‘operator=’ (operand types are ‘const Date’ and ‘std::remove_reference<const                                              Date&>::type {aka const Date}’)
       __b = _GLIBCXX_MOVE(__tmp);
           ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:177:11: note: candidates are:
In file included from date.cc:6:0:
date.h:9:7: note: Date& Date::operator=(const Date&) <near match>
 class Date {
       ^
date.h:9:7: note:   no known conversion for implicit ‘this’ parameter from ‘const Date*’ to ‘Date*’
date.h:9:7: note: Date& Date::operator=(Date&&)
date.h:9:7: note:   no known conversion for argument 1 from ‘std::remove_reference<const Date&>::type {aka const Date}’ to ‘Date&&’
<builtin>: recipe for target 'date.o' failed
make: *** [date.o] Error 1
g++-g-O2-Wall-W-pedantic errors-Wmissing brates-wparenthes-Wold style cast-std=c++11-c-o date.o date.cc
在包含日期的文件中。cc:6:0:
date.h:在构造函数“date::date(int,int,int)”中:
date.h:20:6:警告:'date::day'将在[-Wreorder]之后初始化
国际日;//一天(1-…)
^
date.h:19:6:警告:'int date::month'[-Wreorder]
整月;//月份(1-12)
^
date.cc:24:1:警告:在此处初始化时[-Wreorder]
日期:日期(整数y,整数m,整数d):年(y),日(d),月(m){}
^
在/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_pair.h:59:0中包含的文件中,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_algobase.h:64,
来自/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/char_traits.h:39,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:40,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
从日期开始。抄送:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:在“void std::swap(_Tp&,_Tp&)[with _Tp=const Date]的实例化中:
日期。抄送:77:13:此处为必填项
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:176:11:错误:运算符='不匹配(操作数类型为'const Date'和'std::remove_reference::type{aka const Date})
__a=_GLIBCXX_MOVE(__b);
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:176:11:注:候选项包括:
在包含日期的文件中。cc:6:0:
date.h:9:7:注意:日期和日期::运算符=(const-date&)
上课日期{
^
date.h:9:7:注意:隐式“this”参数从“const date*”到“date*”的转换未知
date.h:9:7:注意:日期和日期::运算符=(日期和)
date.h:9:7:注意:没有已知的参数1从'std::remove_reference::type{aka const date}'到'date&&'的转换
在/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_pair.h:59:0中包含的文件中,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/stl_algobase.h:64,
来自/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/char_traits.h:39,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:40,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
从/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
从日期开始。抄送:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:177:11:错误:运算符='不匹配(操作数类型为'const Date'和'std::remove_reference::type{aka const Date})
__b=移动(tmp);
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/move.h:177:11:注:候选项包括:
在包含日期的文件中。cc:6:0:
date.h:9:7:注意:日期和日期::运算符=(const-date&)
上课日期{
^
date.h:9:7:注意:隐式“this”参数从“const date*”到“date*”的转换未知
date.h:9:7:注意:日期和日期::运算符=(日期和)
date.h:9:7:注意:没有已知的参数1从'std::remove_reference::type{aka const date}'到'date&&'的转换
:目标“date.o”的配方失败
make:**[date.o]错误1
下面是头文件:

/*
 * class Date: describes dates with year, month, and day. Doesn't
 * handle leap years.
 */

#ifndef DATE_H
#define DATE_H

class Date {
public:
    Date();                    // today's date
    Date(int y, int m, int d); // yyyy-mm-dd
    int get_year() const;      // get the year
    int get_month() const;     // get the month
    int get_day() const;       // get the day
    void next();               // advance to next day
private:
    int year;  // the year (four digits)
    int month; // the month (1-12)
    int day;   // the day (1-..)
    static int daysPerMonth[12]; // number of days in each month
};

/*
 * Prints a date in the format yyyy-mm-dd. The function is intended to
 * show an example of a global function; it would be better to overload
 * the output operator <<.
 */
void print(const Date& d);

/*
 * This function is an overloaded operator <. It makes it possible
 * to compare two dates d1 and d2 with 'd1 < d2'.
 */
bool operator<(const Date& d1, const Date& d2);

/*
 * A function to compute the number of days between two dates.
 */
int distance(const Date& d1, const Date& d2);

#endif
/*
*类日期:用年、月和日来描述日期。不是
*处理闰年。
*/
#ifndef日期
#定义日期
上课日期{
公众:
Date();//今天的日期
日期(整数y,整数m,整数d);//yyyy-mm-dd
int get_year()const;//获取年份
int get_month()const;//获取月份
int get_day()const;//获取日期
void next();//前进到第二天
私人:
int year;//年份(四位数)
int month;//月份(1-12)
int day;//日期(1-…)
静态int daysPerMonth[12];//每个月的天数
};
/*
*以yyyy-mm-dd格式打印日期。该函数旨在
*显示一个全局函数的示例;最好重载
*输出运算符
您的问题是
d1
d2
都是
const reference
类型。因为它们是引用,如果可以修改,就可以移动它们。但是,因为它们是
const
,所以不允许修改它们。这意味着它们既不能移动也不能
swap()
ed

也许你想交换
date1
date2

您的问题是
d1
d2
都是
const reference
类型。因为它们是引用,如果可以修改,就可以移动它们。但是,因为它们是
const
,所以不允许修改它们。这意味着它们既不能移动也不能
swap()
ed

也许你想交换
date1
date2

您的问题是
d1
d2
都是
const reference
类型。因为它们是引用,如果可以修改,就可以移动它们。但是,因为它们是
const
,所以不允许修改它们。这意味着它们既不能移动也不能
swap()
ed

也许你想交换
date1
date2

您的问题是
d1
d2
都是
const reference
类型。因为它们是引用,如果可以修改,可以移动它们。Ho
/*
 * Class Date, implementation.
 * The next() function in this implementation cannot handle leap years.
 */

#include "date.h"

#include <ctime>   /* for C routines time and localtime */
#include <iostream>
#include <utility> /* for swap */

using namespace std;

int Date::daysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Date::Date() {
    time_t timer = time(0); // time in seconds since 1970-01-01
    tm* locTime = localtime(&timer); // broken-down time
    year = 1900 + locTime->tm_year;
    month = 1 + locTime->tm_mon;
    day = locTime->tm_mday;
}

Date::Date(int y, int m, int d) : year(y), day(d), month(m) {}

int Date::get_year() const {
    return year;
}

int Date::get_month() const {
    return month;
}

int Date::get_day() const {
    return day;
}

void Date::next() {
    if(month == 12 && day == daysPerMonth[month-1] ){ 
        day=1;
        month = 1;
        year++;
    } else if (month == daysPerMonth[month-1]) {
        month++;
        day = 1;
    } else if(day < daysPerMonth[month-1]){ 
        day++;  
    }
}

void print(const Date& d) {
    cout << d.get_year() << "-";
    if (d.get_month() < 10) { 
        cout << "0";
    }
    cout << d.get_month() << "-";
    if (d.get_day() < 10) {
        cout << "0";
    }
    cout << d.get_day();
}

bool operator<(const Date& d1, const Date& d2) {
    return (d1.get_year() < d2.get_year()) ||
    (d1.get_year() == d2.get_year() && d1.get_month() < d2.get_month()) ||
    (d1.get_year() == d2.get_year() && d1.get_month() == d2.get_month()
     && d1.get_day() < d2.get_day());
}


int distance(const Date& d1, const Date& d2) {

    Date date1 = d1;
    Date date2 = d2;
    int total = 0;
    if ( d2 < d1 ){
        swap(d1,d2);
    }
    while( date1 < date2){
        date1.next();
        total++;
    }
    return total;
}
int distance(const Date& d1, const Date& d2) {
    /* ... code ... */
        swap(d1,d2);
    /* ... code ... */
}