我正在尝试为学校项目编译我的第一个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
我的cc档案:
/*
* 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文件是正确的。
此致
答案 0 :(得分:3)
int distance(const Date& d1, const Date& d2) {
/* ... code ... */
swap(d1,d2);
/* ... code ... */
}
您的问题是d1
和d2
都是const reference
类型。因为他们可以引用,所以如果可以修改,则可以将其移动。但是,因为他们const
,您不能修改它们。这意味着它们既不能移动也不能swap()
。
也许您打算交换date1
和date2
?