0

I'm trying to understand how to write a function that can modify a struct. I want to add a day to my struct Date. However, the addOneDay function below doesn't work.

My goal is to addOneDay to my birthday, using a function. How do I manipulate the data in the struct to get addOneDay to work?

#include <iostream>

    using namespace std;

    struct Date
    {
        int year;
        int month;
        int day;
    };


    Date addOneDay(const Date& date);

    Date addOneDay(const Date& date)
    {
        Date rdate = date.day+1; /* <- this doesn't work */
        return rdate;
    };

void assignValues(Date& myBirthday)
{
    myBirthday.day = 27;
    myBirthday.month = 1;
    myBirthday.year = 1962;
}

main()
{
    Date x;
    assignValues(x);

    cout << x.month << "/" << x.day << "/" << x.year << endl;

    //addOneDay(x)
};
2
  • Can you post an error message? It saves people from having to compile it to see the error for themselves. Commented Mar 1, 2015 at 23:57
  • Sure, thing. The compiler says: No viable conversion from 'int' to 'Date' Commented Mar 2, 2015 at 0:29

2 Answers 2

3

This line is wrong. As you found out.

Date rdate = date.day+1; /* <- this doesn't work */

Because you are assigning an integer value to a date object and as it stands the compiler doesn't know how to do that conversion.

You need to assign the value back to the day field of Date like

date.day += 1;

This fails because date is const.

If you want the input parameter as const then you must return a new Date object. The one you passed in is const and cannot be altered.

Date addOneDay(const Date& date)
{ 
    Date d(date);
    d.day = date.day + 1;
    return d;
};

This inccurs the cost of a few temporary objects.

If you removed the const restriction, you could use the same date like so.

void addOneDay(Date& date)
{
    date.day += 1;       
};
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I wanted to do it by returning a value.
then return a reference to your original date from addOneDay.
@PaulRooney "hen return a reference" Note that this won't work for the const Date& version again.
Absolutely. I was only suggesting that for the non const
3

"How do I manipulate the data in the struct to get addOneDay to work?"

It doesn't work, because you pass a const reference parameter, which actually means the parameter cannot be changed. The solution is to actually use the return value

Date addOneDay(const Date& date);

Date addOneDay(const Date& date) {
    Date rdate(date);
    rdate.day = rdate.day + 1;
    return rdate;
};

3 Comments

I couldn't get either of these to work in Xcode. It wants "date.day =+ 1".
@TimElhajj "It wants date.day =+ 1." Uhm, what? Are you sure we're talking about c++ and not objective-c or something? Also why did you accept the other answer then, which proposes exactly the same?
I see now that I was mistaken. My apologies. When I attempted to implement this yesterday, I failed, but it was my fault in implementation, not anything you wrote.

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.