简体   繁体   中英

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

i have a main cpp file containing something like this

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. 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.
  2. labourParty.setnationalcampagainManager is missing parens indicating that you want to invoke the method. 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.
  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.

Here are my thoughts after seeing the constructor:

The constructor copies the arguments to the data members but then also invokes the relevant setters. Assuming the setters do the same thing, this is redundant; do one or the other, but not both. This just wastes CPU cycles copying the same data to the same place twice.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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