c ++类成员调用错误C3861

时间:2018-05-06 23:43:57

标签: c++

我有点失落。我知道我错过了一些东西,但是我没有看到它。我在引用未被识别的类成员函数时收到错误C3861。我正在寻找类似问题的其他答案,其中大多数都与原型问题和调用顺序有关。所以,我想知道我搞砸了哪里。我知道我应该看到它,但是本周末我的睡眠时间显着不足。

{

2 个答案:

答案 0 :(得分:1)

您有一个类,但是您没有创建Date对象来使用这些函数。您必须使用日期对象才能使用这些功能。

在你的主要功能中:

int main()
{
    int day = 1;
    int month = 1;
    int year = 2000;
    int addedDays;
    Date today(day, month, year);
    today.displayDate(day, month, year);
    cout << "Enter how many days you would like to add:  ";
    cin >> addedDays;
    cout << endl;
    today.addDays(day, month, year, addedDays);
    today.displayDate(day, month, year);
    return 0;
}

请注意,您的addDays()功能无法正常使用。您可以通过将日期转换为天数,添加然后转换回来来实现方法found here

答案 1 :(得分:0)

头文件

#include "stdafx.h"
#include<iostream>
using namespace std;

class Date
{
private:
    int year;
    int month;
    int day;

public:
    Date();
    Date(int day, int month, int year);
    ~Date();

    void setDate(int day, int month, int year);
    // don't need day, month, year since they are accessible in the class
    void addDays(int addedDays); 
    void displayDate();
};

实施档案

#include "Date.h"

// if no day, month, year specified when object is created, set each of them to 1
Date::Date() : day(1), month(1), year(1) 
{}
Date::Date(int day, int month, int year) 
{
    this->day = day;
    this->month = month;
    this->year = year;
}

Date::~Date() 
{}

void Date::setDate(int day, int month, int year)
{
    this->day = day;
    this->month = month;
    this->year = year;
}

void Date::addDays(int addedDays)
{
    this->day = this->day + addedDays;  // or could do day += addedDays
    if(this->day > 30)            //Test if day function needs to be cycled.
    {
        this->month++;
        this->day = 1;
        if(this->month>12)        //Test if month function needs to be cycled.
        {
            this->year++;
            this->month = 1;
        }
    }
}

void Date::displayDate()
{
    cout << "The current date is:  " << this->day << ", " << this->month << ", "<<      this->year << endl;
}

主cpp文件

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

int main()
{
    int day = 1;
    int month = 1;
    int year = 2000;
    int addedDays = 0;

    // declare a date object
    Date date(day, month, year); 

    // call the displayDate function using the date object
    date.displayDate(); 

    std::cout << "Enter how many days you would like to add:  ";
    std::cin >> addedDays;
    std::cout << endl;

    // add the days by calling the addDays function through the date object
    date.addDays(addedDays);
    date.displayDate();

    return 0;
}

如果有任何错误,请告诉我