C++无效使用非静态成员函数?
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
方法的返回值用作构造函数的参数。这没有任何意义;此时尚未构造该对象,因此您不能使用其方法并期望发生任何合理的事情。 labourParty.setnationalcampagainManager
缺少表示要调用该方法的括号。 这就是"非静态成员函数的无效使用" - 你不能在不调用它的情况下按这样的名称引用非静态成员函数。- 如果您确实调用了此方法,则需要提供所需的参数,但由于第 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
);
相关文章:
- 如何在C++中使用非静态成员函数作为回调函数
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 为什么传递非静态成员函数会导致编译错误?
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 非静态成员函数的 decltype 格式不正确吗?
- std::异步与非静态成员函数
- C++无效使用非静态成员函数?
- 未使用的C++未优化的静态成员函数/变量
- C++:如何返回指向非静态成员函数的指针?
- 使用静态成员函数而不是普通函数是否有任何开销?
- 在 stream_descriptor::async_wait 中无效使用非静态成员函数
- 为什么我不能像这样在静态成员函数中调用静态成员变量?
- 无法在clang Linux中分配非静态成员函数,但我可以在Visual Studio Windows中分配
- 在类定义中推导内联静态成员函数的返回类型
- `enable_if()`以禁用模板化类的静态成员函数声明
- 错误:无效使用非静态成员函数"int test::hotplug_callback(libusb_contex
- 常量静态成员函数
- 使用 vector.sort() 的非静态成员函数无效使用
- 从静态成员函数访问私有非静态类变量 - C++