如何从 C++ class 中的构造函数为 Arduino 分配字符串字段?
How do you assign a string field from a constructor in a C++ class for Arduino?
我正在尝试学习如何使用 class 来保存多个值和 return 来自函数的实例。另一种选择是使用全局变量,这可能更简单,但我想在 C++ 中学习 classes。
唯一给我带来麻烦的是从构造函数参数分配给字符串字段。我已经尝试了这段代码的几种变体,并且 none 可以编译。
这是我目前得到的错误:
在构造函数中'RelayInfo::RelayInfo(int, char*)':
17: 错误:'char*' 到 'char [20]'
的赋值不兼容类型
我已经有很长时间没有在 C 中处理指针等了。
//The RelayInfo class only has public fields and a constructor.
//Its only purpose is to hold these values.
class RelayInfo
{
public:
RelayInfo(int pin, char message[20]);
int Pin;
char Message[20];
};
RelayInfo::RelayInfo( int pin, char message[20] )
{
Pin = pin;
Message = message*;
}
void setup() {
pinMode(13, OUTPUT);
digitalWrite( 13, LOW );
}
void loop() {
//Construct an instance of the class:
RelayInfo info( 13, "Hello, World!" );
//Use the fields from the class.
if( info.Message == "Hello, World!" )
{
digitalWrite( info.Pin, HIGH );
}
}
定义需要是:
RelayInfo( int pin, char* message );
甚至更好:
RelayInfo( int pin, const char* message );
编辑:
此外,您可能应该使用:
strncpy()
用于复制字符指针。
根据 PDizzle745 的建议,我得出以下结论:
class RelayInfo
{
public:
RelayInfo(int pin, const char* message);
~RelayInfo();
int Pin;
char* Message;
};
RelayInfo::RelayInfo( int pin, const char* message )
{
Pin = pin;
//Allocate enough memory for copying the input string.
Message = (char*) malloc( strlen(message) * sizeof(char) + 1 );
//Copy the input string to the class's field.
strcpy( Message, message );
}
RelayInfo::~RelayInfo(void)
{
free( Message );
}
void setup() {
pinMode(13, OUTPUT);
digitalWrite( 13, HIGH );
delay(1000);
}
void loop() {
RelayInfo info( 13, "Hello" );
if( strcmp( info.Message, "Hello" ) == 0 )
{
digitalWrite( info.Pin, LOW );
}
}
我正在尝试学习如何使用 class 来保存多个值和 return 来自函数的实例。另一种选择是使用全局变量,这可能更简单,但我想在 C++ 中学习 classes。
唯一给我带来麻烦的是从构造函数参数分配给字符串字段。我已经尝试了这段代码的几种变体,并且 none 可以编译。
这是我目前得到的错误:
在构造函数中'RelayInfo::RelayInfo(int, char*)': 17: 错误:'char*' 到 'char [20]'
的赋值不兼容类型我已经有很长时间没有在 C 中处理指针等了。
//The RelayInfo class only has public fields and a constructor.
//Its only purpose is to hold these values.
class RelayInfo
{
public:
RelayInfo(int pin, char message[20]);
int Pin;
char Message[20];
};
RelayInfo::RelayInfo( int pin, char message[20] )
{
Pin = pin;
Message = message*;
}
void setup() {
pinMode(13, OUTPUT);
digitalWrite( 13, LOW );
}
void loop() {
//Construct an instance of the class:
RelayInfo info( 13, "Hello, World!" );
//Use the fields from the class.
if( info.Message == "Hello, World!" )
{
digitalWrite( info.Pin, HIGH );
}
}
定义需要是:
RelayInfo( int pin, char* message );
甚至更好:
RelayInfo( int pin, const char* message );
编辑:
此外,您可能应该使用:
strncpy()
用于复制字符指针。
根据 PDizzle745 的建议,我得出以下结论:
class RelayInfo
{
public:
RelayInfo(int pin, const char* message);
~RelayInfo();
int Pin;
char* Message;
};
RelayInfo::RelayInfo( int pin, const char* message )
{
Pin = pin;
//Allocate enough memory for copying the input string.
Message = (char*) malloc( strlen(message) * sizeof(char) + 1 );
//Copy the input string to the class's field.
strcpy( Message, message );
}
RelayInfo::~RelayInfo(void)
{
free( Message );
}
void setup() {
pinMode(13, OUTPUT);
digitalWrite( 13, HIGH );
delay(1000);
}
void loop() {
RelayInfo info( 13, "Hello" );
if( strcmp( info.Message, "Hello" ) == 0 )
{
digitalWrite( info.Pin, LOW );
}
}