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

Compiling C++ on Windows (Mingw / Cygwin)

本文关键字:Mingw Cygwin C++ Windows 编译      更新时间:2023-10-16

我正在尝试为学校项目编译我的第一个 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

这是头文件:

/*
 * 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

还有我的抄送文件:

/*
 * 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;
}

有什么我忘记了什么吗?

这是课程中的第一个练习,应该非常简单 - 这让我相信标题和 make 文件是正确的。

问候

int distance(const Date& d1, const Date& d2) {
    /* ... code ... */
        swap(d1,d2);
    /* ... code ... */
}

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

也许你的意思是交换date1date2