在字符串之前预期')'

时间:2013-03-08 15:54:53

标签: 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

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

#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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:10)

问题#1:string

添加包含指令
#include <string>

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

using std::string;

问题#3:您无法重新分配this指针:

this = new Date(monthStr, dd, yy); // ERROR!

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

*this = Date(monthStr, dd, yy);

答案 1 :(得分:2)

在代码的开头使用std::string或声明using namespace std;