gpt4 book ai didi

c++ - '.setDay' 左侧的错误 C2228 必须具有类/结构/union

转载 作者:行者123 更新时间:2023-11-30 04:43:10 25 4
gpt4 key购买 nike

我的项目是我们在类里面构建的项目的修改版。例如,它应该打印用户输入的日期并将其转换为一年中的日期。 “2 月 12 日”= 43但它一直给我一个 .setDay 错误。我的老师指示我把它放在那里,但我的代码停止工作,他也无济于事。它还消除了错误 E0299 无法确定重载函数“DayOfYear::DayOfYear”的实例。

修改.cpp

    // 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com