简体   繁体   中英

invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream<char>') and 'void')

i am using eclipse on mac to run c++ program. I am new to c++ and was trying to learn composition by using different classes individually.I am facing the issue in the following line of the code

Main.cpp

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

int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    Birthday obj(25,3,1993);
    obj.print();
    People pp(5,obj);
    pp.printinfo();
    return 0;
}

Birthday.cpp

#include <iostream>
using namespace std;
#include "Birthday.h"
//#include "People.h"

Birthday::Birthday(int d,int m,int y){
    // TODO Auto-generated constructor stub
    date =d;
    month=m;
    year=y;
}

void Birthday::print()
        {

          cout <<date << month<<year<<endl;
        }

People.h

#ifndef PEOPLE_H_
#define PEOPLE_H_
//using namespace std;
#include "Birthday.h"


class People {
public:
    People(int x,Birthday bb);
    void printinfo();

private:
    int xx;
    Birthday bo;
};


#endif /* PEOPLE_H_ */

People.cpp

#include "People.h"
#include <iostream>
using namespace std;
#include "Birthday.h"
#include<string>
People::People(int x,Birthday bb)
:xx(x),bo(bb)
{
    // TODO Auto-generated constructor stub

}

void People::printinfo()
{

cout<< xx<<bo.print(); //I am getting error because of this line , as soon as i comment it program compiles fine.
}

I have tried to use string variable instead of xx variable but it was giving me some other error.So i tried to simplify and learn the concept of compostion before jumping into strings manipulation directly.

cout << xx << bo.print();

bo.print() - function and they not have return value (void)

Just write: cout << xx; bo.print();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM