[英]C++ invalid use of non-static member function?
我有一个主 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
labourParty
方法的返回值用作构造函数的 arguments。 这没有任何意义; 此时 object 尚未构建,因此您不能使用其方法并期望发生任何合理的事情。labourParty.setnationalcampagainManager
缺少指示您要调用该方法的括号。 这是“非静态成员函数的无效使用”——你不能在不调用它的情况下按这样的名称引用非静态成员 function。看到构造函数后,这是我的想法:
构造函数将 arguments 复制到数据成员,但随后也调用相关的设置器。 假设 setter 做同样的事情,这是多余的; 做一个或另一个,但不能同时做。 这只会浪费 CPU 周期,将相同的数据复制到同一个地方两次。
由于您按值获取 arguments,您还可以移动构造数据成员,这将节省一些 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
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.