- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个 arduino 项目,我一直在使用以下模板通过 << 运算符打印各种数据类型
template<class T>
inline Print &operator <<(Print &obj, T arg)
{ obj.print(arg); return obj; }
一直很好,直到尝试处理使用 Arduinos P 宏存储的字符数组,Arduinos P 宏将数据存储在闪存而不是 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);
}
最佳答案
在您的第一个示例中,您正在调用 printP(const prog_uchar*)
参数类型为 const prog_uchar[12]
可以简单地转换为 const prog_uchar*
.
当您使用 operator<<
时,您正在调用函数 print()
使用类型为 const prog_uchar[12]
的对象但你没有任何print
适合这种类型的过载。因此,编译器寻找不同的 print
重载并采取最合适的。但是,在您的案例中不仅有一个“最合适”的重载,而且有 8 个,因此您最终会收到详细的错误消息。
您的问题的解决方案是将您尝试打印的内容显式转换为 const char*
:
P(CT_PLAIN) = "text/plain\n";
server << (const char*) CT_PLAIN;
另一种解决方案是提供给 print
const prog_uchar*
的过载.
关于C++ << 运算符重载和 Arduino 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108721/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!