首页 > 解决方案 > .h 中的 C++ C2065 变量无法从 .cpp 文件访问

问题描述

Green coder 在这里进行第一次 C++ 作业,我正在寻求帮助以解决我出错的地方。谷歌搜索了这个错误/情况,但没有找到足够接近的东西来找到解决方案。

任务是创建一个日期类,然后使用提供的 .cpp 文件对其进行测试。我把整个事情写出来并编译,得​​到了令人沮丧的错误数量,所以先注释掉大部分内容以首先调试核心,我发现错误基本上是说 date.cpp 找不到 date 中定义的变量。 H。谷歌搜索似乎表明这是一个包含错误,但我包含了 date.h。

有一次,我有 date.h 在大写之前定义了月、日、年,并且 IDE(VS 2019 社区)建议包括(可能拼写错误),所以我将变量名大写。

在此先感谢您的帮助。我假设(希望?)我在这里有一个很容易整理的概念问题。

// Program Name: date.h
#include <string>

#ifndef DATE_H
    #define DATE_H
#endif

// Start of class

class Date {

public: 
    explicit Date(unsigned int = 1, unsigned int = 1, unsigned int = 2000); // default ctor setting to 1/1/2000 per specs
    std::string tointString() const; // date string in month/day/year format
    std::string toFullString() const; // date string in date fullMonth year format
    ~Date(); // Gotta have the dtor

    // get functions
    unsigned int getMonth();
    unsigned int getDay();
    unsigned int getYear();

    std::string getMonthName() const;

private: // Core date data   Book seems to want to use unsigned. Does this force non-negagive #s?

    unsigned int Month; // month of year 1-12
    unsigned int Day; // day of month 1-31 (leap year adjustments not required in this assignment)
    unsigned int Year; // Will be using 1900 as the minimum year.

    unsigned int checkDate(int, int); // Function to validate the days in a month

}; // end of Date

这是 date.cpp 发生错误的地方。错误在最后的三个“get”函数中。 date.cpp(47,34):错误 C2065:“月份”:未声明的标识符

// 
// Program Name; date.cpp
// Assignment: Date object assignment
//

#include "date.h" // class include file
#include <iostream> // standard output
#include <sstream> // formatted output
#include <string> // work with strings
#include <array> // needed for various arrays in get functions

using namespace std; // save all the scope fun

unsigned int const monthsInYear{ 12 }; // var just to make code more readable.

// Parameter based constructor w/ value checks
//
Date::Date(unsigned int mon, unsigned int dy, unsigned int yr) 
// initialization list and start of value checking  
    : Month{ mon }, Day{ checkDate(dy , mon ) }, Year{ yr } { 
    if (mon < 1 || mon > monthsInYear) {
        Month = 1; // if month out of range, set to 1 per specs
    } // end of month value check if
    if (yr < 1900 || yr > 2100) { // arbitrary upper limit
        Year = 1900;
    } // end of year value check if

} // end initialization list

int checkDate(unsigned int dayToCheck , unsigned int mon ) {
    static array <unsigned int, monthsInYear> daysInMonth{ 31,28,31,30,31,30,31,31,30,31,30,31 }; // Array showing how many days in each month
    unsigned int dayToReturn{ 1 }; // Value to return. Per specs, if dayToCheck is out of range, we will return 1.

    if (dayToCheck >= 1 && dayToCheck <= daysInMonth[mon - 1]) { // needs to be month -1 re: index starts at 0
        dayToReturn = dayToCheck; // dayToCheck inside the proper range, so set dateToReturn to that value.
    } // end if block for day in month range check.

    return dayToReturn; // returning the value.

} // end of dateCheck

// Functions for retrieving data
//
unsigned int getMonth() { return Month; }
unsigned int getDay() { return Day; }
unsigned int getYear() { return Year; }

标签: c++

解决方案


正如您编写getMonth的那样,该 .cpp 文件中的其他函数只是与 .cpp 无关的独立函数Date

你需要写unsigned int Date::getMonth()和类似地让其他人表达他们是其中的一部分Date


推荐阅读