简体   繁体   English

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

[英]C++ invalid use of non-static member function?

i have a main cpp file containing something like this我有一个主 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);

However when i compile the main body code i get an error like such.但是,当我编译主体代码时,出现这样的错误。

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){

which corresponds to the following line of code对应于以下代码行

 void Party::setnationalcampagainManager(nationalcompaignManager NationalcompaignManangerObject){

         newNationalcompaignMananger = NationalcompaignManangerObject;

     }

Im not quite sure what might be causing this error as ive never recieved it before.我不太确定是什么导致了这个错误,因为我以前从未收到过它。 If any more code is needed i am happy to provide.如果需要更多代码,我很乐意提供。 thankyou:)谢谢你:)

Party Constructor派对建设者

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());
     }

There's a few issues with your constructor invocation:您的构造函数调用存在一些问题:

Party labourParty(
    labourParty.getPartyName(),
    labourParty.getLeader(),
    labourParty.getCandidate(),
    labourParty.getCampagainManager(),
    labourParty.getNationalCampgainManager(),
    labourParty.setnationalcampagainManager,
    labourParty.getStance()
); //create 1st politcal Party
  1. You are using the return value of labourParty methods as arguments to the constructor.您将labourParty方法的返回值用作构造函数的 arguments。 This does not make any sense;这没有任何意义; the object is not yet constructed at this point, so you cannot use its methods and expect anything reasonable to happen.此时 object 尚未构建,因此您不能使用其方法并期望发生任何合理的事情。
  2. labourParty.setnationalcampagainManager is missing parens indicating that you want to invoke the method. labourParty.setnationalcampagainManager缺少指示您要调用该方法的括号。 This is the "invalid use of non-static member function" -- you can't reference a non-static member function by name like this without invoking it.这是“非静态成员函数的无效使用”——你不能在不调用它的情况下按这样的名称引用非静态成员 function。
  3. If you do invoke this method, you need to supply the required argument, but you can't even invoke the method yet because of point 1.如果您调用此方法,则需要提供所需的参数,但由于第 1 点,您甚至无法调用该方法。

Here are my thoughts after seeing the constructor:看到构造函数后,这是我的想法:

The constructor copies the arguments to the data members but then also invokes the relevant setters.构造函数将 arguments 复制到数据成员,但随后调用相关的设置器。 Assuming the setters do the same thing, this is redundant;假设 setter 做同样的事情,这是多余的; do one or the other, but not both.做一个或另一个,但不能同时做。 This just wastes CPU cycles copying the same data to the same place twice.这只会浪费 CPU 周期,将相同的数据复制到同一个地方两次。

Since you take the arguments by value, you could also move-construct the data members, which will save some CPU cycles making copies of data that is just going to be destroyed anyway.由于您按值获取 arguments,您还可以移动构造数据成员,这将节省一些 CPU 周期来制作无论如何都会被销毁的数据副本。 I suspect you could apply this same optimization to the setters.我怀疑您可以将同样的优化应用于设置器。

As far as actually invoking the constructor, you can replace all of this code:至于实际调用构造函数,您可以替换所有这些代码:

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);

With just this:仅此而已:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM