小编Tim*_*ski的帖子

C++ <<运算符重载和Arduino模板

我正在开发一个arduino项目,我使用以下模板使用<<运算符打印各种数据类型

template<class T>
inline Print &operator <<(Print &obj, T arg)
{ obj.print(arg); return obj; }
Run Code Online (Sandbox Code Playgroud)

在尝试处理使用Arduinos P宏存储的char数组之前一直没有问题,后者将数据存储在flash而不是ram中

//params stored in flash using P() from webduino library
P(CT_PLAIN) = "text/plain\n";
server << CT_PLAIN;
Run Code Online (Sandbox Code Playgroud)

这导致编译器错误

httpServer.h : : In function 'Print& operator<<(Print&, T) [with T = const prog_uchar*]':
httpServer.cpp : instantiated from here
httpServer.h : call of overloaded 'print(const prog_uchar*&)' is ambiguous
Run Code Online (Sandbox Code Playgroud)

虽然以下编译

//params stored in flash using P() from webduino library
P(CT_PLAIN) = "text/plain\n";
server.printP(CT_PLAIN);
Run Code Online (Sandbox Code Playgroud)

我试图创建一个<<运算符重载,但我不完全理解语法和方法,我已经研究了几个小时无济于事,并将高度感谢任何反馈.

 WebServer &operator <<(WebServer &server,const prog_uchar …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading arduino

3
推荐指数
1
解决办法
1651
查看次数

带有条件语句的c ++中的sprintf()

我正在尝试使用sprintf()为C++中的arduino项目格式化一个char数组我的目标是格式化以下整数:日,月,年,小时,分钟和秒进入以下

DD/MM/YYYY HH:MM:SS

当一个整数<10时,我的问题就出现了.我尝试用下面的sprintf修改格式

 sprintf (timeStr, "%c%u/%c%u/%u %c%u:%c%u:%c%u",(monthDay>0 && monthDay<=9)?'0':'',monthDay,(month>0 && month<=9)?'0':'',month,year,(hour>0 && hour<=9)?'0':'',hour,(minute>0 && minute<=9)?'0':'',minute,(second>0 && second<=9)?'0':'',second);
Run Code Online (Sandbox Code Playgroud)

现在这不会编译,因为我得到"空字符常量",我认为它来自''没有合法价值.

如果语句中的%c是有条件的并且我只想要一个值<10,那么我就会被困在如何使用sprintf来格式化字符串

如果有人对我如何实现这一目标有任何了解,我会非常感激,因为我真的被它坚持了!

谢谢!

c c++ arduino

1
推荐指数
1
解决办法
1953
查看次数

标签 统计

arduino ×2

c++ ×2

c ×1

operator-overloading ×1

templates ×1