在我的cpp或头文件中找不到错误,类构造失败

时间:2019-05-12 06:42:29

标签: c++ visual-studio class

首先,我对C ++还是陌生的,只有很少的教导/实践,因此请记住这一点。我为正在处理的项目创建了Date类。我以前是用草率的方式来组织代码,但是它的功能足以使我有效地编写代码的语法。有人查看了我的代码后,我意识到我需要更好地构造类,因此尝试将Date类组织到头文件和cpp文件中。这样做之后,我遇到了许多错误:

  

'day': undeclared identifier
  missing type specifier - int is assumed

另外,由于cpp文件中的日期在Visual Studio中会更改颜色,因此日期也被识别为一种类型,但是在头文件中,该类并未将其着色为一种类型。

一位导师仔细检查了一下,不知道我的错误来自何处,但是如果我删除了这两个文件,我的代码功能就不会出错,因此肯定在下面的脚本中。 A

我已经尝试从头开始重建整个项目,因为我最初以为这是目录问题,但是经过一丝不苟的操作并完全确保我没有放错文件,所以我看不到它将如何因此。

Date.h

#pragma once

#ifndef DATE_H
#define DATE_H

class Date
{
public:
    Date(int y, int m, int d);

    Date();

    const int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    int yearsDifference();

    int daysDifference();

    int totalDays();

private:
    int year;
    int month;
    int day;

};

#endif 

Date.cpp

#ifndef DATE_H
#define DATE_H

#include <Date.h>
#include <iostream>
#include <string>



Date::Date(int y, int m, int d)
{
    y = year;
    m = month;
    d = day;
}

Date::Date()
{
    year = 0;
    month = 0;
    day = 0;
}

static Date today() {
    struct tm ti;
    time_t t = time(0);
    localtime_s(&ti, &t);
    int y = 1900 + ti.tm_year;
    int m = 1 + ti.tm_mon;
    int d = ti.tm_mday;
    return Date(y, m, d);
}



int Date::yearsDifference()
{

    bool laterInYear = (month > today().month)
        || (month == today().month && day > today().day);

    int result = year - today().year;
    if (!laterInYear)
    {
        result--;
    }
    return result;
}



int Date::daysDifference()
{

    int todayMonthDays = 0;
    int maturityMonthDays = 0;

    for (int i = 0; i < (month - 1); i++) {
        maturityMonthDays += monthDays[i];
    }


    for (int i = 0; i < (today().month - 1); i++) {
        todayMonthDays += monthDays[i];
    }

    maturityMonthDays += day;
    todayMonthDays += today().day;

    bool laterInYear = (month > today().month)
        || (month == today().month && day > today().day);

    int result;
    if (laterInYear)
    {
        result = maturityMonthDays - todayMonthDays;
    }
    else
    {
        result = 365 - (todayMonthDays - maturityMonthDays);
    }
    return result;
}

int Date::totalDays()
{
    int result = (yearsDifference() * 365)
        + daysDifference();
    return result;
}

#endif

任何帮助将不胜感激,我已经盯着这个看了好几个小时试图修复它,但我看不到它。

2 个答案:

答案 0 :(得分:2)

您必须删除.cpp文件中的#ifdef防护。

这是因为#include通过复制n粘贴整个头文件来工作。并且由于在包含Date.h标头之前先定义了DATE_H,所以DATE_H也已在Date.h中定义(然后有效地禁用了enitre标头)。

答案 1 :(得分:0)

Data类的构造函数应该像这样

[]
0: {title: "Henk WJ Ovink", description: "Special Envoy for International Water Affairs, Kin…ands, and Sherpa to the High Level Panel on Water", slug: "http://siwidev.websearchpro.net/press/", image: "http://siwidev.websearchpro.net/wp-content/uploads/2019/03/636385501414087698.png", imageWidth: 1903, …}
1: {title: "Amina J. Mohammed", description: "Deputy Secretary-General United Nations", slug: "http://siwidev.websearchpro.net/community/", image: "http://siwidev.websearchpro.net/wp-content/uploads/2019/03/h20fddcc.jpg", imageWidth: 776, …}
2: {id: 8342, title: "Panaceas or painkillers – what role for sustainability assessment tools?", description: null, slug: "8342-panaceas-or-painkillers---what-role-for-sustainability-assessment-tools", image: "https://siwi.websearchpro.net/Content/ProposalReso…g-2019-8342-tn-img-2019-8605-iStock-500553193.jpg", …}
3: {id: 8380, title: "Inclusive Policy and Governance for Water and Sanitation ", description: null, slug: "8380-inclusive-policy-and-governance-for-water-and-sanitation", image: "https://siwi.websearchpro.net/Content/ProposalReso…019-8420-img-2019-8420-org-InkedPhoto IWRM_LI.jpg", …}
4: {id: 8464, title: "Cities4Forests: 50 cities commit to forests citing water benefits", description: null, slug: "8464-cities4forests-50-cities-commit-to-forests-citing-water-benefits", image: "https://siwi.websearchpro.net/Content/ProposalReso…464-tn-img-2019-8481-WESCA illegal FS dumping.jpg", …}
5: {id: 8474, title: "Urban water resiliency: a coordinated response from source to settlement ", description: null, slug: "8474-urban-water-resiliency-a-coordinated-response-from-source-to-settlement", image: "https://siwi.websearchpro.net/Content/ProposalResources/Image/Default/default-www-tn.jpg", …}
6: {id: 8526, title: "Including all: participatory approaches in water governance and programmes ", description: null, slug: "8526-including-all-participatory-approaches-in-water-governance-and-programmes", image: "https://siwi.websearchpro.net/Content/ProposalReso…ge/2019/Thumbnail/img-2019-8526-tn-Field trip.JPG", …}
length: 7
__proto__: Array(0)

还删除cpp文件中的#ifdef