首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于CLASS的public跟private

2013-03-26 
关于CLASS的public和private#include IOSTREAMclass CData{private:int m_nDay// Lets assume all mont

关于CLASS的public和private

#include <IOSTREAM>

class CData
{
private:
int m_nDay;// Lets assume all months have 30 days!
int m_nMonth;
int m_nYear;

void AddDays(int nDaysToAdd)
{
m_nDay = m_nDay + nDaysToAdd;

if (30 < m_nDay)
{
AddMonths(m_nDay / 30);

m_nDay %= 30;
}
}

void AddMonths (int nMonthsToAdd)
{
m_nMonth = m_nMonth + nMonthsToAdd;

if (12 < m_nMonth)
{
AddYears(m_nMonth / 12);

m_nMonth = m_nMonth % 12;
}
}

void AddYears(int m_nYearsToAdd)
{
m_nYear = m_nYear + m_nYearsToAdd;
}

public:
CDate(int nDay, int nMonth, int nYear)
: m_nDay(nDay), m_nMonth(nMonth), m_nYear(nYear) {};

CData& operator ++()
{
CData mReturnDate(m_nDay, m_nMonth, m_nYear);

AddDays(1);

return mReturnDate;
}

void DisplayDate()
{
std::cout << m_nDay << " / " << m_nMonth << " / " << m_nYear;
}
};

int main(void)
{
CDate mDate(25, 6, 2008);

std::cout << "The date object is initialized to: ";
mDate.DisplayDate();
std::cout << std::endl;

++mDate;

std::cout << "Date after prefix-increment is: ";

mDate.DisplayDate();
std::cout << std::endl;

return 0;
}

我这样定义的类为什么不能通过编译的?
public只能在private的上面吗?
编译错误:
cpp(41) : error C2327: 'CData::m_nDay' : member from enclosing class is not a type name, static, or enumerator
cpp(41) : error C2065: 'm_nDay' : undeclared identifier
cpp(41) : error C2065: 'nDay' : undeclared identifier
[解决办法]
你太粗心了。。
代码中有多处CData 和 CDate。。你修改下先。。

热点排行