【发布时间】:2013-06-14 12:41:43
【问题描述】:
我正在开发一个 arduino 项目,我一直在使用以下模板通过 打印各种数据类型
template<class T>
inline Print &operator <<(Print &obj, T arg)
{ obj.print(arg); return obj; }
在尝试处理使用 Arduinos P 宏存储的 char 数组之前一直很好,该宏将数据存储在闪存而不是 ram 中
//params stored in flash using P() from webduino library
P(CT_PLAIN) = "text/plain\n";
server << CT_PLAIN;
导致编译错误
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
虽然以下编译
//params stored in flash using P() from webduino library
P(CT_PLAIN) = "text/plain\n";
server.printP(CT_PLAIN);
我试图创建一个
WebServer &operator <<(WebServer &server,const prog_uchar *str)
{ server.printP(str); }
template<class T>
inline Print &operator <<(Print &obj, T arg)
{ obj.print(arg); return obj; }
虽然我仍然遇到同样的编译器错误。
WebServer::printP 的声明是
void printP(const prog_uchar *str);
非常感谢任何反馈和帮助!
完整的编译器错误:
Compiling 'webapp' for 'Arduino Mega 2560 or Mega ADK'
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
Print.h : print(const String&) <near match>
Print.h : print(const char*) <near match>
Print.h : print(char) <near match>
Print.h : print(unsigned char, int) <near match>
Print.h : print(int, int) <near match>
Print.h : print(unsigned int, int) <near match>
Print.h : print(long int, int) <near match>
Print.h : print(long unsigned int, int) <near match>
Error compiling
另外定义WebServer::printP
void WebServer::printP(const prog_uchar *str)
{
// copy data out of program memory into local storage, write out in
// chunks of 32 bytes to avoid extra short TCP/IP packets
uint8_t buffer[32];
size_t bufferEnd = 0;
while (buffer[bufferEnd++] = pgm_read_byte(str++))
{
if (bufferEnd == 32)
{
m_client.write(buffer, 32);
bufferEnd = 0;
}
}
// write out everything left but trailing NUL
if (bufferEnd > 1)
m_client.write(buffer, bufferEnd - 1);
}
【问题讨论】:
-
真的是
server.printP(CT_PLAIN);还是你的意思是server.print(P(CT_PLAIN));?第一种情况,P(CT_PLAIN)的类型是什么,printP的签名是什么? -
是的,我仔细检查了它的
server.printP(CT_PLAIN);CT_PLAIN is static const prog_uchar CT_PLAIN[12] = "text/plain\n"和printP方法是void WebServer::printP(const prog_uchar *str) -
Print和WebServer是什么关系? -
WebServer类减速class WebServer: public Print
标签: c++ templates operator-overloading arduino