Mitro
Mitro

Reputation: 1260

Error: no match for operator<<

I'm training with C++ and classes. My project is composed by two file:

main.cpp

using namespace std;

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

main(){
  Date date1;
  Date date2;
  cout<<"Type first date: ";
  date1.setAll();
  cout<<"type second date: ";
  date2.setAll();

  cout<<First date: "<<date1.getS();
  cout<<Second date: "<<date2.getS();
}

Date.h

class Date{
public:
  Date(){}
  ~Date(){system("pause");}
  void setAll();
  struct dmy{
    int day, month, year;
  };
  dmy c;
  dmy getS();
private:
  void setDay();
  void setMonth();
  void setYear();
};

void Date::setAll(){
  setDay();
  setMonth();
  setYear();
}

//all set ...

Date::dmy Date::getS(){
  return c;
}

I get errors in the main at

cout<<"First date: "<<date1.getS();

The error message starts with

Error: no match for 'operator<<' in std::operator<<

What does this error mean, and how can I fix it?

Due to internet restrictions on our schools' PCs I cannot copy the error message directly, here's a screen of the error message in DevC++:

errors

Upvotes: 0

Views: 456

Answers (2)

Karthik Kalyanasundaram
Karthik Kalyanasundaram

Reputation: 1525

date1.getS() actually returns the struct Date::dmy. To make your code compile you should overload operator<< for Date::dmy

Upvotes: 1

Marius Bancila
Marius Bancila

Reputation: 16328

The ostream class does not have the operator<< overloaded for your struct dmy. Therefore it does not know how to print that value. What you need without overloading the operator is something like this:

Date::dmy date = date1.getS();
cout<<"First date: "<< date.year << "." << date.month << "." << date.day;

Upvotes: 4

Related Questions