0

我在mac上使用eclipse来运行c++程序。我是 C++ 新手,并试图通过单独使用不同的类来学习组合。我在以下代码行中遇到了问题

主文件

#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;
}

生日.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;
        }

人.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_ */

人.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 变量,但它给了我一些其他错误。所以在直接跳入字符串操作之前,我尝试简化和学习组合的概念。

4

1 回答 1

1

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

bo.print() - 函数并且它们没有返回值(void)

只需写: cout << xx; bo.print();

于 2019-04-18T21:14:24.050 回答