[英]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.