如何解决Visual C ++无法将我的函数识别为类的一部分
我正在制作一个不错的程序,该程序可以有任何日期,并可以找到它的前一天和后一天。我已经完成了所有工作,除了调试时,我遇到了13个错误!阅读错误语句后,我发现该函数未被识别为类的一部分,但我不知道为什么。
我觉得自己了解自己在做什么,但是有些不对劲。如果有人有任何建议,请告诉我。谢谢!
我有三个文件:
主文件(Source.cpp):
#include <iostream>
#include <iomanip>
#include <string>
#include "DateType.h"
using namespace std;
int main() {
//Inputs
int month,day,year;
bool flag;
DateType today,tomorrow,yesterday;
do { //Registers input and checks if it can go through program
cout << "Enter the current month (1-12): ";
cin >> month;
cout << "Enter the current year: ";
cin >> year;
cout << "Enter in today's date: ";
cin >> day;
today.SetDate(month,year);
flag = today.valid_date();
} while (!flag);
//Equalizing all three objects
tomorrow = today;
yesterday = today;
//Moving ford and back one day
tomorrow.NextDay();
yesterday.PreviousDay();
//Output
cout << "Given Date: " << today.print_Day() << endl;
cout << "Next Date: " << tomorrow.print_Day() << endl;
cout << "Previous Date: " << yesterday.print_Day() << endl;
system("pause");
return 0;
}
头文件(DateType.h)
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
enum months { Jan=31,Feb = 28,March = 31,April = 30,May = 31,June = 30,July = 31,August = 31,Sept = 30,Oct = 31,Nov = 30,Dec = 31,Feb_Leap = 29 };
class DateType {
private:
int mo;
int day;
int yr;
months DaysInMonth;
void Day_Month_Assign(); //This function does the same thing as DaysinMonth() on the assignment sheet,using the enum
public:
DateType(); //Constructor to set mo=0,day=0,yr=1000
void SetDate(int,int,int); //Date is set according to the incoming parameter
void NextDay(); //Date is increased by one to get NextDay
void PreviousDay(); //Date is decreased by one day to get Previous Day
string print_Day(); //Returns a string value
bool valid_date(); //Checks if an input is a valid date
};
DateType.cpp文件
#include <iostream>
#include <cstdlib>
#include "DateType.h"
#include <string>
using namespace std;
DateType::DateType() {
mo = 0;
day = 0;
yr = 1000;
}
void DateType::Day_Month_Assign() {
switch (mo) {
case 1: DaysInMonth = Jan;
case 2: DaysInMonth = Feb;
case 3: DaysInMonth = March;
case 4: DaysInMonth = April;
case 5: DaysInMonth = May;
case 6: DaysInMonth = June;
case 7: DaysInMonth = July;
case 8: DaysInMonth = August;
case 9: DaysInMonth = Sept;
case 10: DaysInMonth = Oct;
case 11: DaysInMonth = Nov;
case 12: DaysInMonth = Dec;
}
if ((yr % 4 == 0 && yr % 100 != 0) ||
(yr % 400 == 0))
DaysInMonth = Feb_Leap;
}
void DateType::SetDate(int month,int days,int year) {
mo = month;
day = days;
yr = year;
Day_Month_Assign();
}
void DateType::NextDay() {
day++;
if (day > DaysInMonth) { //Raises month
day = 1;
mo++;
Day_Month_Assign(); //Assigns new value to enum
}
if (mo > 12) {
mo = 1;
yr++;
Day_Month_Assign();
}
}
void DateType::PreviousDay() {
day--;
//Different order than the last one
//I need to get the days in month from enum first
//That will be the new day value
if (day == 0) {
mo--;
Day_Month_Assign();
day = DaysInMonth;
}
if (mo == 0) {
yr--;
mo = 12;
Day_Month_Assign();
day = DaysInMonth;
}
}
string DateType::print_Day() {
string date;
date = (to_string(mo) + "-" + to_string(day) + "-" + to_string(yr));
return date;
}
bool DateType::valid_date() {
bool flag;
if (day <= DaysInMonth && day >= 1) { //Days in Month Check
if (mo > 0 && mo <= 12) { //Month in Year Check
flag = true;
cout << "Valid Date,Accepted!\n";
}
else {
flag = false;
cout << "Invalid Month!\n";
}
}
else {
flag = false;
cout << "Invalid Date!\n";
}
return flag;
}
编辑:所讨论的函数是String DateType::print_Day()
函数。错误如下:
主文件:
'print_Day' is not a member of 'DateType' (Called 3 Times),C2039
CPP文件:
'print_Day' is not a member of 'DateType' (Line 73,C2039) 'mo': undeclared identifier (Line 75,C2065) 'day': undeclared identifier (Line 75,C2065) 'yr': undeclared identifier (Line 75,C2065) declaration is incompatible with " DateType::print_Day()" (declared at line 21 of Header File) (Line 73,E0147)
头文件:
'print_Day': unknown override specifier (Line 21,C3646) syntax error: '(' (Line 21,C2059) unexpected token(s) preceding ';' (Line 21,C2238)
这三个头文件的所有错误稍后在错误列表中再次引发。我希望这会有所帮助!
解决方法
在DateType.h中,字符串未知,因为字符串类属于std名称空间。要解决该错误,请将print_Day的定义更改为
std::string print_Day()
请勿尝试通过在页眉中添加using namespace std;
来解决此问题,因为这样做不会导致问题继续下去。
关于编程风格的小注释:在DateType.cpp中,最好先包含DateType.h。这样,您将知道DateType.h没有依赖项,并且可以包含在任何cpp文件中。
,这没有解决问题,但没有命名变量flag
。给变量名称告诉您它们的作用。在这种情况下,date_is_valid
尽管有点长,但更加清晰。但是您不需要它。只需进行循环测试while(!today.valid_date())
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。