在字符串之前预期')'

LSa*_*age 0 c++ compiler-errors codeblocks

我已经为这个问题尝试了很多不同的解决方案,我似乎无法弄清楚为什么编译器继续在我的头文件中给我这个错误.如果有人能给我一些见解,那将非常感激.编辑:抱歉忘了哪一行给出错误.它在头文件中的行:Date(string mstr,int dd,int yy);

是的,我知道这个=新的日期...是一个糟糕的解决方案,我只是稍微研究一下;)

标题:

#include <string>

#ifndef DATE_H
#define DATE_H

class Date{
    public:
        Date(int mm, int dd, int yy);
        Date(string mstr, int dd, int yy);
        void print();
        void printFullDate();
        void prompt();

        void setMonth(int);
        void setDay(int);
        void setYear(int);

        int getMonth();
        int getDay();
        int getYear();

        static const int monthsPerYear = 12;
    private:
        int month;
        int day;
        int year;

        int checkDay(int);
};

#endif
Run Code Online (Sandbox Code Playgroud)

如果你需要它,这是实现(它还没有完全完成,我只是试图测试我写的一些函数):

#include <iostream>
#include <stdexcept>
#include "Date.h"
using namespace std;

Date::Date(int mm, int dd, int yy){
    setMonth(mm);
    setYear(yy);
    setDay(dd);
}

Date::Date(string mstr, int dd, int yy){
    cout << "It's working";
}

int Date::getDay(){
    return day;
}

int Date::getMonth(){
    return month;
}

int Date::getYear(){
    return year;
}

void Date::setDay( int dd ){
    day = checkDay(dd);
}

void Date::setMonth( int mm ){
    if( mm > 0 && mm <= monthsPerYear)
        month = mm;
    else
        throw invalid_argument("month must be 1-12");

}

void Date::setYear( int yy ){
    year = yy;
}

int Date::checkDay( int testDay){
    static const int daysPerMonth[ monthsPerYear + 1 ] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if( testDay > 0 && testDay <= daysPerMonth[ getMonth() ])
        return testDay;
    if( getMonth() == 2 && testDay == 29 && (getYear() % 400 == 0 || ( getYear() % 4 == 0 && getYear() % 100 != 0 ) ) )
        return testDay;
    throw invalid_argument("Invalid day for current month and year");
}

void Date::print(){

}

void Date::printFullDate(){

}

void Date::prompt(){
    int userChoice = 1;
    int mm, dd, yy;
    string monthStr;

    while(userChoice != 3){
        cout << "Enter 1 for format: MM/DD/YYYY" << endl;
        cout << "Enter 2 for format: Month DD, YYYY" << endl;
        cout << "Enter 3 to exit" << endl;
        cout << "Choice: " << endl;
        cin >> userChoice;
        while(userChoice < 1 || userChoice > 3){
            cout << "Please enter a number 1 - 3 for the formats above." << endl;
            cout << "Choice: " << endl;
            cin >> userChoice;
        }
        if(userChoice != 3){
            switch(userChoice){
                case 1:
                    cout << "Enter Month (1 - 12): ";
                    cin >> mm;
                    setMonth(mm);
                    cout << "Enter Day of Month: ";
                    cin >> dd;
                    setDay(dd);
                    cout << "Enter Year: ";
                    cin >> yy;
                    setYear(yy);
                    break;
                case 2:
                    cout << "Enter Month Name: ";
                    cin >> monthStr;
                    cout << "Enter Day of Month: ";
                    cin >> dd;
                    cout << "Enter Year: ";
                    cin >> yy;
                    this = new Date(monthStr, dd, yy);
                    break;
                default:
                    break;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*owl 10

问题#1:添加一个include指令string

#include <string>
Run Code Online (Sandbox Code Playgroud)

问题2:使用完全限定std::string而不仅仅是string,或在类定义之前放置using声明:

using std::string;
Run Code Online (Sandbox Code Playgroud)

问题3:你无法重新指定this指针:

this = new Date(monthStr, dd, yy); // ERROR!
Run Code Online (Sandbox Code Playgroud)

您要做的事情可能应该重写为:

*this = Date(monthStr, dd, yy);
Run Code Online (Sandbox Code Playgroud)

  • @LSavage:`this`指针是指向当前对象的指针.即使你可以改变它,它也不会改变对象本身,只改变指针.但无论如何你都无法改变它,它是不可改变的(你真的希望它如此!).在类的每个成员函数中,您知道`this`指向调用成员函数的对象.另一方面,`*this`取消引用指针以获取对当前对象的引用,并且`operator =`是(隐式)为类型为`Date`的对象定义的.用几句话我无法解释任何更好,尝试谷歌搜索一些教程;) (4认同)