C++无效使用非静态成员函数?

C++ invalid use of non-static member function?

本文关键字:静态成员 函数 无效 C++      更新时间:2023-10-16

我有一个包含类似以下内容的主 cpp 文件

Leader labourLeader("George Lopez",100,50,50, 75);//sets record for the labour party leader
Candidate labourCandidate("Donna Smith",100,50,50, 75);
compaignMananger labourCampagainManagr("John Gunn",100,50);
nationalcompaignManager labourNationalCampagainManager("Adam Lapel",100,50);
natioanlfinancialManager labourFinancialManager("Sandra Bullac",100,50);
Party labourParty(labourParty.getPartyName(), labourParty.getLeader(), labourParty.getCandidate(), labourParty.getCampagainManager(), labourParty.getNationalCampgainManager(),
labourParty.setnationalcampagainManager, labourParty.getStance()); //create 1st politcal Party
labourParty.setLeader(labourLeader);
labourParty.setCandidate(labourCandidate);
labourParty.setcampaginManager(labourCampagainManagr);
labourParty.setnationalcampagainManager(labourNationalCampagainManager);
labourParty.setnationalFinancalManager(labourFinancialManager);
labourParty.setPartyName("Labour");
labourParty.setStance(1000);

但是,当我编译主体代码时,我收到这样的错误。

error: invalid use of non-static member function ‘void Party::setnationalcampagainManager(nationalcompaignManager)’
labourParty.setnationalcampagainManager, labourParty.getStance()); //create 1st politcal Party
note: declared here
void Party::setnationalcampagainManager(nationalcompaignManager NationalcompaignManangerObject){

对应于以下代码行

void Party::setnationalcampagainManager(nationalcompaignManager NationalcompaignManangerObject){
newNationalcompaignMananger = NationalcompaignManangerObject;
}

我不太确定可能导致此错误的原因,因为我以前从未收到过它。如果需要更多代码,我很乐意提供。谢谢:)

派对构造函数

class Party{
public:
std::string partyName;
int initalStance;
Leader newLeader;
Candidate newCandidate;
compaignMananger newcompaignMananger ;
nationalcompaignManager newNationalcompaignMananger;
natioanlfinancialManager newNationalfinancialManager;
Party(std::string partyName, Leader newLeader, Candidate newCandidate, compaignMananger newcompaignMananger,
nationalcompaignManager newNationalcompaignMananger, natioanlfinancialManager newNationalfinancialManager, int initalStance) {
this->partyName = partyName;
this->newLeader = newLeader;
this-> newNationalcompaignMananger = newNationalcompaignMananger;
this-> newcompaignMananger = newcompaignMananger;
this-> newNationalfinancialManager = newNationalfinancialManager;
this->newCandidate = newCandidate;
this->initalStance = initalStance;
setPartyName(getPartyName());
setLeader(getLeader());
setCandidate(getCandidate());
setcampaginManager(getCampagainManager());
setnationalcampagainManager(getNationalCampgainManager());
setnationalFinancalManager(getFinancialManager());
setStance(getStance());
}

构造函数调用存在一些问题:

Party labourParty(
labourParty.getPartyName(),
labourParty.getLeader(),
labourParty.getCandidate(),
labourParty.getCampagainManager(),
labourParty.getNationalCampgainManager(),
labourParty.setnationalcampagainManager,
labourParty.getStance()
); //create 1st politcal Party
  1. 您将labourParty方法的返回值用作构造函数的参数。这没有任何意义;此时尚未构造该对象,因此您不能使用其方法并期望发生任何合理的事情。
  2. labourParty.setnationalcampagainManager缺少表示要调用该方法的括号。 这就是"非静态成员函数的无效使用" - 你不能在不调用它的情况下按这样的名称引用非静态成员函数。
  3. 如果您确实调用了此方法,则需要提供所需的参数,但由于第 1 点,您甚至还不能调用该方法。

以下是我看到构造函数后的想法:

构造函数将参数复制到数据成员,但随后还会调用相关的资源库。 假设二传手做同样的事情,这是多余的;做一个或另一个,但不能同时做两个。 这只会浪费 CPU 周期将相同的数据复制到同一位置两次。

由于您按值获取参数,因此您还可以移动构造数据成员,这将节省一些 CPU 周期,以复制无论如何都会被销毁的数据。 我怀疑您可以将相同的优化应用于二传手。

就实际调用构造函数而言,您可以替换所有这些代码:

Party labourParty(labourParty.getPartyName(), labourParty.getLeader(), labourParty.getCandidate(), labourParty.getCampagainManager(), labourParty.getNationalCampgainManager(),
labourParty.setnationalcampagainManager, labourParty.getStance()); //create 1st politcal Party
labourParty.setLeader(labourLeader);
labourParty.setCandidate(labourCandidate);
labourParty.setcampaginManager(labourCampagainManagr);
labourParty.setnationalcampagainManager(labourNationalCampagainManager);
labourParty.setnationalFinancalManager(labourFinancialManager);
labourParty.setPartyName("Labour");
labourParty.setStance(1000);

就这样:

Party labourParty(
"Labour",
labourLeader,
labourCandidate,
labourCampagainManagr,
labourNationalCampagainManager,
labourFinancialManager,
1000
);