Resolving a linker error: undefined reference to static class members

≡放荡痞女 提交于 2019-11-29 14:45:43

You have to define your class statics in a source file some where. Putting them in the class just declares that they are there, but something still needs to define them.

Inside your .cpp file your can do so like this:

NewSoftSerial SerialServoControl::_serial(9, 8);
char SerialServoControl::_tx = 0;
char SerialServoControl::_rx = 0;

Put appropriate initial values; I just assumed the comment was for the constructor.

You need to create memory and initialize your static variables.

In your CPP file add the following:

NewSoftSerial SerialServoControl::_serial(9, 8);
char SerialServoControl::_tx = 0;
char SerialServoControl::_rx = 0;

Because the value _serial is static, it exists when an object isn't instantiated. This means that you must declare it in the code as

NewSoftSerial SerialServoControl::_serial(9, 8);
char SerialServoControl::_tx = 0;
char SerialServoControl::_rx = 0;

which has already been suggested.

If you then want to change it in the init function, you simply have to change your _serial = ... line to SerialServoControl::_serial = NewSoftSerial(tx, rx). Of course, this means you'll have to define a relevant constructor for the NewSoftSerial class.

Hope this helps.

I know this is somewhat dead, but in my case I forgot to define the methods I was referencing, imagine that.......

Yes they were DECLARED, but when I checked my .cpp file, there was no definition for them, so this time the error was literal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!