- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经看过几个关于这个的问题,特别是 Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’很有帮助。它让我知道我的问题是我正在做一些 c++11 无法从中推断出类型的事情。
我认为我的问题的很大一部分是我正在使用的实例化类是模板化的,但最初是从指向非模板基类的指针中获得的。这是我从另一个关于如何将模板类对象放入 STL 容器的 stackoverflow.com 问题中建议的。
我的课:
class DbValueBase {
protected:
virtual void *null() { return NULL; } // Needed to make class polymorphic
};
template <typename T>
class DbValue : public DbValueBase {
public:
DbValue(const T&val) { data = new T(val); }
~DbValue() { if (data) delete data; }
T *data;
const T& dataref() const { return *data; }
friend std::ostream& operator<<(std::ostream& out, const DbValue<T>& val)
{
out << val.dataref();
return out;
}
}
还有,编译错误 database.cc:530:90: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
的代码片段发生:
//nb: typedef std::map<std::string,DbValueBase*> DbValueMap;
const CommPoint::DbValueMap& db_values = cp.value_map();
for (auto i = db_values.cbegin() ; i != db_values.cend() ; i++) {
// TODO: Need to implement an ostream operator, and conversion
// operators, for DbValueBase and DbValue<>
// TODO: Figure out how to get a templated output operator to
// work...
// DbValue<std::string> *k = dynamic_cast<DbValue<std::string>*>(i->second);
std::cerr << " Database field " << i->first << " should have value " << *(i->second) << endl;
}
如果我的输出尝试打印 i->second
,它编译并运行,我看到了指针。如果我尝试输出 *(i->second)
,我得到编译错误。在 gdb 中单步执行时,似乎仍然知道 i->second
是正确的类型
(gdb) p i->second
$2 = (DbValueBase *) 0x680900
(gdb) p *(i->second)
warning: RTTI symbol not found for class 'DbValue<std::string>'
$3 = warning: RTTI symbol not found for class 'DbValue<std::string>'
{_vptr.DbValueBase = 0x4377e0 <vtable for DbValue<std::string>+16>}
(gdb) quit
我希望我做错了什么。但是,它比我自己似乎能够弄清楚的要复杂得多。其他人看到我做错了什么或不完整的事情吗?
编辑:
@PiotrNycz 确实为我在下面提出的问题提供了一个很好的解决方案。然而,尽管目前在进行开发时打印值,但对这些 DbValue<>
的真正需求对象是让它们返回正确类型的值,然后我可以将其提供给数据库操作方法。我应该在我最初的问题中提到,打印是有值(value)的,但不是我目标的终点。
最佳答案
如果你想通过基指针打印对象 - 在基类中创建 ostream 运算符:
class DbValueBase {
protected:
virtual ~DbValueBase() {}
virtual void print(std::ostream&) const = 0;
friend std::ostream& operator << (std::ostream& os, const DbValueBase & obj)
{
obj.print(os); return os;
}
};
template <typename T>
class DbValue : public DbValueBase {
public:
void print(std::ostream& os) const
{
out << dataref();
}
};
关于c++ - 错误 : cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13347530/
我的 friend 给我发了一个代码,他说在 Windows 中成功编译。我在linux上尝试过,但未能给出以下错误。下面是代码的最小可验证示例。 #include #include using
搜索这个问题的标题给了我很多人引用相同的错误,但在不同的情况下,不幸的是那里提供的答案是针对他们的情况的,我看不出他们能如何帮助我。 我正在尝试重载 operator template class
我已经看过几个关于这个的问题,特别是 Overloading operator&&’很有帮助。它让我知道我的问题是我正在做一些 c++11 无法从中推断出类型的事情。 我认为我的问题的很大一部分是我正
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
在这段代码中,我尝试将迭代器移动 10 个元素。 #include #include #include int main() { using
你好everonye即使我在这里找到了很多关于这个问题的答案,但一个人根本无法向我解释事情的进展如何无法解决它。 所以我的问题是我有一个名为 Matrix 的类,当我尝试将运算符 }’ lvalue
如何在 C++ 中打印 high_resolution_clock? #include #include typedef std::chrono::high_resolution_clock hi
关于这个标题的 StackOverflow 有很多关于这个标题的问题,但没有一个对我有帮助,我真的不明白发生了什么。 我正在尝试创建一个生成随机单词的类。首先,我尝试将所有元音和辅音放在两个不同的 s
我在尝试做一些简单的事情时遇到了一个令人困惑的错误消息 std::cout {1,2,3}; 上面写着 cannot bind 'std::ostream {aka std::basic_ostre
我在尝试编译我的代码时遇到此错误: 1> c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/ostream: In constructor 'Log
我想知道如何在 std::basic_ostream 中插入参数我一直在努力,但我做不到 我需要插入一个参数来选择我想要打印的 arista 值一旦我插入了参数,下一步就很容易了,因为它只是一个 if
根据 cppref , 有一个 operator 过载接受 const char* .似乎转换操作只是加宽了每个 char进入 wchar_t .也就是说,转换(插入)的宽字符数等于窄字符数。那么问
为什么使用用户定义的类进行流操作的典型 header C通常是这样的: std::ostream& operator>(std::istream& is, C&); 不是这样的: template
我想提供一个流运算符来输出 std::chrono::time_point 作为 GMT 日期,我目前有以下内容(仅针对 ostream 进行了简化): using datetime_t = std:
我正在阅读 basic_ostream 对象的 write 方法,这是我在 cppreference 上找到的内容: basic_ostream& write( const char_type* s,
这个问题在这里已经有了答案: Misunderstanding about ostream class and operator << (5 个答案) 关闭 8 年前。 标题是我的问题。 basic
我收到以下错误,我不确定是什么问题 1 IntelliSense: "std::basic_ostream::basic_ostream(const std::basic_ostream::_Myt
上面有转换吗? 最佳答案 将std::string 转换为输出 流?通常是:将字符串转换为从给定字符串中读取字符的输入流: std::string myString = //... std::istr
就在我认为我了解 C++11 中 iostream 的多遍移动构造时(感谢 https://stackoverflow.com/a/8156356/273767 的介绍),我遇到了这个: §27.7.
§27.7.3.9为 operator basic_ostream& operator&& os, const T& x); Effects: os > 的右值重载。) 基本上,它只是转发到左
我是一名优秀的程序员,十分优秀!