在Windows上编译C++(Mingw / Cygwin)
Compiling C++ on Windows (Mingw / Cygwin)
我正在尝试为学校项目编译我的第一个 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 ... */
}
您的问题是d1
和d2
都是const reference
类型。因为它们是引用的,所以如果可以修改,它们可以被移动。但是,因为它们是 const
,因此不允许修改它们。这意味着它们既不能被移动也不能被swap()
编辑。
也许你的意思是交换date1
和date2
?
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 如何将C++中的库和头与MinGW一起使用
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 无法在windows上使用mingw将sqlite3与c连接
- 在cygwin中运行c++脚本时发生错误
- 在cygwin中测试新的boost安装时出现cpp错误
- MinGw 无法从 cmake 编译测试文件
- MinGW在Cygwin上.关于链接GNU科学图书馆的一些问题
- 我可以在计算机上同时在计算机上同时使用Cygwin和Mingw吗?
- 在cygwin中使用mingw编译器编译c ++程序时缺少dll。
- 如何更改为使用cygwin下的g++mingw工具链
- 如何在Cygwin和MinGW中告诉C++库路径
- 在Windows上编译C++(Mingw / Cygwin)
- 使用cygwin从Nmake切换到MinGW
- std::thread在cygwin中工作,但在MinGw中不起作用
- rand_r不在作用域中,gcc-mingw在windows上的cygwin下
- 为什么需要Cygwin和MinGW ?
- 我怎样才能把水冲干净?(环境:Mingw编译器,运行在xterm或minty的Cygwin)
- 在cygwin中编译新的mingw二进制文件