我在Mac上使用Eclipse运行C ++程序。我是c ++的新手,并试图通过单独使用不同的类来学习合成。我在代码的以下行中遇到了问题
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.
}
我尝试使用字符串变量而不是xx变量,但这给了我一些其他错误。因此,在直接进入字符串操作之前,我尝试简化并学习了组合的概念。
答案 0 :(得分:0)
cout << xx << bo.print();
bo.print()-函数并且它们没有返回值(无效)
只需写: cout << xx; bo.print();