C++ operator overloading and accessing private data variables(C++运算符重载和访问私有数据变量)
本文介绍了C++运算符重载和访问私有数据变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C++的新手,我正在编写一个实现日期函数的类。
该程序有两个非成员函数bool printDate(const Date& d)
和string intToString(const int& n)
,以及两个用于重载运算符<< and >>
指向代码的链接为https://repl.it/NC2H/37
我一直收到类似
的错误'std::__cxx11::string Date::month' is private within this context `,
` ambiguous overload for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const Date')`
and
`note: declared private here`
编码:
#include<iostream>
#include<vector>
#include<string.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
const vector<string> months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
string intToString(const int& n){
return to_string(n); //inbuilt function
}
class Date{
//Private members
string month;
int day;
int year;
bool isValidMonth() const{
return day==daysInMonth(m);
}
int daysInMonth(const string& m) const{
vector<string>::iterator m_index;
if(find(months.begin(),months.end(),m)!=months.end()){
m_index=find(months.begin(),months.end(),m);
}
int month_index=distance(months.begin(),m_index)+1;
switch(month_index)
{
case 2: return isLeapYear()?29:28;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
}
bool isLeapYear() const{
if(year%4==0){
if(year%100==0 && year%400==0){ //2000 is leap year but 1900 is not
return true;
}
else{
return false;
}
}
else{
return false;
}
}
string toString(){
string result_date;
result_date=intToString(day)+"-"+month+"-"+intToString(year);
return result_date;
}
public:
friend istream& operator >>(istream& is, Date& d);
friend ostream& operator <<(ostream&os ,const Date);
//public constructor
Date(const string& m="January",const int& d=1,const int&y=2000){
month=m;
day=d;
year=y;
}
void setMonth(const string& m){
month=m;
}
void setDay(const int& d){
day=d;
}
void setYear(const int& y){
year=y;
}
string getMonth()const{
return month;
}
int getDay() const{
return day;
}
int getYear() const{
return year;
}
void Month(){
if(!month.empty())
{
month[0]=toupper( month[0] );
for(int i=1;i<month.length();++i)
month[i]=tolower(month[i]);
}
}
bool isValidDate() const{
//calls isValidMonth() to check if days match
if(isValidMonth() ){
return true;
}
return false;
}
};
istream & operator >>(istream & is, Date& d){
//overloads the >>operator, which reads
cout<<"
Enter the day of the date
";
cin>>d.day;
cout<<"
Enter the month of the date
";
cin>>d.month;
cout<<"
Enter the year of the date
";
cin>>d.year;
return is;
}
ostream & operator <<(ostream & os ,const Date& d){
os<<d.day<<"."<<d.month<<"."<<d.year;
return os;
}
bool printDate(const Date& d){
// if(!d.isValidDate()){
if(!d.isValidDate()){
cerr<<"( "<<d<<" ): not valid date";
return false;
}
cout<<"( "<<d<<" )";
return true;
}
int main(int argc, char const *argv[])
{
// code for testing purposes
Date defaultDate;
printDate(defaultDate);
cout<<endl;
/*
Date moonLanding("jul",20,1969);
printDate(moonLanding);
cout<<endl;
moonLanding.setMonth("july");
printDate(moonLanding);
cout<<endl;
Date leapDay("Ferbruary",29,2001);
printDate(leapDay);
cout<<endl;
leapDay.setDay(28);
printDate(leapDay);
cout<<endl;
Date d;
cout<<d.getMonth()<<" "<<d.getDay()<<", "<<d.getYear<<endl;
d.setMonth("July");
d.setDay(4);
d.setYear(1776);
printDate(d);
cout<<endl;
while(cin>>d){
d.Month();
bool flag=printDate(d);
cout<<endl;
if(flag) cout<<d.toString()<<endl;
}
*/
return 0;
}
程序运行时的错误为:
main.cpp: In function 'std::ostream& operator<<(std::ostream&, const Date&)':
main.cpp:133:17: error: 'int Date::day' is private within this context
os<<d.day<<"."<<d.month<<"."<<d.year;
^~~
main.cpp:19:14: note: declared private here
int day;
^~~
main.cpp:133:29: error: 'std::__cxx11::string Date::month' is private within this context
os<<d.day<<"."<<d.month<<"."<<d.year;
^~~~~
main.cpp:18:17: note: declared private here
string month;
^~~~~
main.cpp:133:43: error: 'int Date::year' is private within this context
os<<d.day<<"."<<d.month<<"."<<d.year;
^~~~
main.cpp:20:14: note: declared private here
int year;
^~~~
main.cpp: In function 'bool printDate(const Date&)':
main.cpp:139:23: error: ambiguous overload for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const Date')
cerr<<"( "<<d<<" ): not valid date";
~~~~~~~~~~^~~
main.cpp:132:19: note: candidate: std::ostream& operator<<(std::ostream&, const Date&)
ostream & operator <<(ostream & os ,const Date& d){
推荐答案
友元声明与实际运算符不匹配:
friend ostream& operator <<(ostream&os ,const Date);
...
ostream & operator <<(ostream & os ,const Date& d)
请注意,实际运算符接受引用。
这篇关于C++运算符重载和访问私有数据变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:C++运算符重载和访问私有数据变量
基础教程推荐
猜你喜欢
- C++,'if' 表达式中的变量声明 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 设计字符串本地化的最佳方法 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31