0

I'm trying to compile my first c++11 file for a school project, but I'm getting several errors in both cygwin and mingw environments, Maybe I'm missing some part I need to install but this should be a pretty straightforward thing. Alas, I know nothing of C++ compiling so I have no idea where to start looking.

This is the output i get:

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

Here's the header file:

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

And my cc file:

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

Is there anything I've forgotten about?

This is the first exercise in the course and should be very straightforward - which leads me to believe that the header and make files are correct..

Regards

2
  • same failure with mingw? what is your g++ -version? Commented Jan 23, 2015 at 16:39
  • Yes same failure. Cygwin: gcc version 4.9.2 (GCC) mingw: gcc version 4.9.2 (tdm64-1) Commented Jan 23, 2015 at 16:44

1 Answer 1

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

Your issue is that d1 and d2 are both const reference types. Because they're reference, they could be moved if they can be modified. However, because they're const, you are not allowed to modify them. That means that they can neither be moved nor swap()ed.

Perhaps you meant to swap date1 and date2?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.