我有三个C++类:Position,Employer和Person.每个人都有雇主和就业岗位.如下所示,我使用前向类声明将Employer和Position类加入Person类.
我是新的转发类的声明,但是发现何时使用前向声明帖子对于何时以及如何使用前向声明非常有见地.
但是,我主要关心如何在我的main函数中使用setPosition()?
person.h
class Position;
class Employer;
class Person
{
public:
// other public members
void setPosition(Employer* newC, Position* newP)
{
m_position = newP;
m_employer = newC;
}
private:
// other member variables
Position* m_position;
Employer* m_employer;
};
Run Code Online (Sandbox Code Playgroud)
以下是main.cpp的片段:
#include "employer.h"
#include "person.h"
#include "position.h"
int main()
{
Employer StarFleet("StarFleet Federation", "space exploration");
Person JLP("Jean-Luc Picard");
Position cpt("StarFleet Captain", "Save the world");
JLP.setPosition(StarFleet,cpt);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是得到一个编译错误:
错误:main.cpp候选中对'Person :: setPosition(Employer&,Position&)'的调用没有匹配函数:void Person :: setPosition(Employer*,Position*)
从"雇主"到"雇主*"的参数1没有已知的转换
我想知道如何在main()中使用setPosition? …