我的项目是对我们在类里面构建的项目的修改。例如,应该打印用户输入的日期,并将其转换为一年中的数字天。 “2月12日” = 43
但它一直给我一个.setDay错误。我的老师指示我把它放在那里,我的代码停止工作了,他无济于事。这也使错误E0299无法确定打算使用哪个重载函数“DayOfYear::DayOfYear”的实例。
修改文件
// Headers
#include <iostream>
#include <cstdlib>
#include <string>
#include"DayOfYear.h"
using namespace std;
int main()
{
// create an instance of a class
DayOfYear dayOfYearObj("Feburary", 12);
string month;
int day; // to hold the day
// display the purpose of the program
cout << "This program converts a string into a number representing what\n" <<
"number in the year the date falls on as well as the day before and after the date provided.\n";
// get the unout from the user
cout << "\nEnter in a month and a date: ";
cin >> month, day;
dayOfYearObj;
// diplsy the object
dayOfYearObj.print();
cout << endl;
system("PAUSE");
return 0;
}
DayOfYear.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class DayOfYear
{
private:
int day;
public:
static int daysAtEndOfMonth[];
static string monthName[];
DayOfYear(string, int);
void print();
void setDay(int day) { this->day = day; }
};
DayOfYear.cpp
#include "DayOfYear.h"
int DayOfYear::daysAtEndOfMonth[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
string DayOfYear::monthName[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December" };
DayOfYear::DayOfYear(string name, int d)
{
int day = 0; //calculated value of date
if (name == "January" )
{
setDay(d);
}
else if (name == "February")
{
day = daysAtEndOfMonth[0] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "March")
{
day = daysAtEndOfMonth[1] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "April")
{
day = daysAtEndOfMonth[2] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "May")
{
day = daysAtEndOfMonth[3] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "June")
{
day = daysAtEndOfMonth[4] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "July")
{
day = daysAtEndOfMonth[5] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "August")
{
day = daysAtEndOfMonth[6] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "September")
{
day = daysAtEndOfMonth[7] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "October")
{
day = daysAtEndOfMonth[8] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "November")
{
day = daysAtEndOfMonth[9] + d;
DayOfYear::DayOfYear.setDay(d);
}
else if (name == "December")
{
day = daysAtEndOfMonth[10] + d;
DayOfYear::DayOfYear.setDay(d);
}
else
{
cout << "Number is out of range." << endl;
}
}
// dayofYear print convert and print day of year
void::DayOfYear::print()
{
int month = 0;
while (daysAtEndOfMonth[month] < day)
month = (month + 1) % 12;
if (month == 0)
cout << "January" << day << endl << endl;
else
{
cout << endl << monthName[month] << " " << day - daysAtEndOfMonth[month - 1]
<< endl << endl;
}
}
最佳答案
setDay
不是静态的,它需要从该类的实例中调用,所以您不能像这样调用它
DayOfYear::DayOfYear.setDay(d);
相反,您可以从
this
调用它this->setDay(d);
这是在类方法中隐式调用的,因此您只需删除
this->
setDay(d);
实际上,您可以对
"January"
正确执行此操作,然后在接下来的几个月中不正确。关于c++ - '.setDay'左边的错误C2228必须具有类/结构/union ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58435902/