Error: expected primary expression before token << <<

Ok so I am building the Date class

class Date
{
    private:
    int d;
    int m;
    int y;

 public:
    Date();
    Date(int&, int&, int&);
    Date(const Date&);
    bool isLeap();
    void setDay(int& day)
    {d = day;}
    void setMonth(int& month)
    {m = month;}
    void setYear(int& year)
    {y = year;}
    int getDay() const
    {return d;}
    int getMonth() const
    {return m;}
    int getYear() const
    {return y;}
    void print(ofstream& outfile)
    {
        outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;
    }

    Date operator= (const Date&);
};

      

I created the main thread and opened the file. I will also be sure to pass it on via the link! Every time I try to compile, I get the error: expected primary expression before '<<token message ... Here is my main function. Let me know if I need anything else.

    int main()
{
    int day;
    int month;
    int year;
    Date d1;
    ofstream outfile("Print.txt");
    int maxDays;

    Date d2 = d1;

    cout << "Please enter the 4 digit year: ";
    cin >> year;
    if (year > 0)
    {
        d1.setYear(year);
        cout << "Please enter the 2 digit month: ";
        cin >> month;
        if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            maxDays = 31;
        else if (month==4||month==6||month==9||month==11)
            maxDays = 30;
        else if (month==2&&d1.isLeap()==false)
            maxDays = 28;
        else if (month==2&&d1.isLeap()==true)
            maxDays = 29;
        if (month < 12)
        {
            d1.setMonth(month);
            cout << "Please enter the 2 digit day: ";
            cin >> day;
            if (day <= maxDays)
                d1.setDay(day);
            else
            {
                cout << "Invalid input, will set to default." << endl;
                d1 = d2;
            }


        }
        else
        {
            cout << "Invalid input, will set to default." << endl;
            d1 = d2;
        }
    }

    else
    {
        cout << "Invalid input, will set to default." << endl;
        d1 = d2;
    }

    d1.print(outfile);

    if(d1.isLeap()==true)
        cout << "This is a leap year\n";
    if(d1.isLeap()==false)
        cout << "This is not a leap year\n";

    cout << "The date is " << d1.getDay() << "/" << d1.getMonth() << "/" << d1.getYear() << endl;

    outfile.close();

    return 0;
}

      

Any help would be greatly appreciated ...

+3


source share


2 answers


This line:

outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;

      

Has two <<

with nothing in between.



Editor’s Note: I am assuming that you are using GCC based on the error message you mentioned. You might be interested in trying clang , which gives a much better error message in this case:

example.cpp:30:54: error: expected expression
    outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;
                                                     ^
1 error generated.

      

If you are a beginner, it can save you a lot of time chasing Bizzarro GCC error messages that can be generated with minor syntax errors.

+4


source


Not what you asked, but if you twist this a little:

    if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        maxDays = 31;
    else if (month==4||month==6||month==9||month==11)
        maxDays = 30;
    else if (month==2&&d1.isLeap()==false)
        maxDays = 28;
    else if (month==2&&d1.isLeap()==true)
        maxDays = 29;

      

Like this:



    if (month==2)
    {  
       if (d1.isLeap())
         maxDays = 29;
       else
         maxDays = 28; 
    }
    else if (month==4||month==6||month==9||month==11)
        maxDays = 30;
    else
        maxDays = 31;

      

your code becomes a little easier to read in my opinion.

0


source







All Articles