使成员函数静态会使程序无法编译。想不通为什么
Making a member function static makes the program fail to compile. Can't figure out why
我正在为arduino制作一个程序。我正在使用avr-g+4.9.2与STL从这里。
我有一节课鸡尾酒。我希望所有Cocktail类型的对象都能够访问指针向量。这些指针指向Alcohol类型的对象。由于这个指针向量对于Cocktail的每个实例都是相同的,所以我想使它成为静态的。但是,如果我使它们成为静态的,我的程序就无法编译。这是代码:
Ineb.hpp
class Alcohol
{
private:
float flow_rate_;
Pump * which_pump_;
public:
std::string this_alcohol_;
Alcohol(std::string this_alcohol, float flow_rate);
Alcohol(std::string this_alcohol, float flow_rate, Pump which_pump);
float HowLong(float percentage_of_drink, uint8_t drink_size); //How long in seconds the pump should be on
void ChangeByteToRegister(uint8_t& byte_to_register);
};
class Cocktail
{
private:
bool order_matter_;
uint8_t byte_to_register_;
static std::vector<Alcohol*> alcohol_directory_;
public:
static void test(Alcohol *ba) {alcohol_directory_.push_back(ba);} //STATIC KEYWORD HERE
Cocktail(bool ordr_matter);
std::vector<std::string> GetIngredients(const uint8_t& num_ingredients, PGM_P& string_table);
uint8_t GetByteToRegister();
void MakeDrink(const uint8_t& num_ingredients, PGM_P& string_table);
};
main.cpp
#include "src/Ineb.hpp"
#include "src/Pins.hpp"
#include "ingredients.h"
#include <pnew.cpp>
extern "C" void __cxa_pure_virtual() {
for(;;);
}
int main(void) {
init();
setup();
Ineb::Pump A(1,8);
Ineb::Alcohol Vodka("vodka", 2.5, A);
Ineb::Cocktail::test(&Vodka);
for(;;)
loop();
return 0; // not reached
}
未定义对`Ineb::Cocktail::alcohol_directory_'的引用
我主要感到困惑的是,当我去掉静态时,为什么会编译它。静电在引擎盖下做什么??
static
成员必须在类定义之外定义。
具有线路
std::vector<Alcohol*> Cocktail::alcohol_directory_;
在Cocktail.cpp
中将执行此操作。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 二叉排序树无法编译
- [UE4错误]无法编译项目.你想在Visual Studio中打开它吗?
- 我只是想让我的代码正确编译
- Libnoise 不想编译:找不到 -lnoise
- 我想使用 CMake 生成和编译 Visual Studio projet,我需要从构建中排除单元测试文件。有没有办法做到这一点?
- 在Visual Studio 2013中编译的STL列表代码在Visual Studio 2019中给出了错误.想知道原
- 在没有 -g 选项的情况下编译,但我想获得更详细的调试信息
- 开关大小写程序在某个点上保持循环.想不通
- 在Visual Studio中,我不想提交我的Debug文件夹,但它中有dll文件,如果我移动它,项目将无法编译
- 我想在Windows上使用Clang从C++编译64位代码-找不到stdio.h文件
- 已编译的应用想要从绝对路径加载 DLL
- 如果我只想要架构良好的工作区,但不一定是分开的编译,如何在C++中组织头/代码文件
- 散列!!答案错误,想不通
- STL 迭代器不起作用?想不通为什么
- 能够编译和运行.然而,当运行代码时,它不能打印我想打印的任何值
- 模板功能,想不通
- C++代码编译,但在运行时出现分段错误。我想我正在篡改链表
- 当我想在外面捕获某些东西时无法编译 lambda
- 使成员函数静态会使程序无法编译。想不通为什么