Visual C ++无法将我的函数识别为类的一部分

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-