草庐IT

C++ : friend function in a template class for operator<<

在.cpp文件中声明模板类的友元函数(对于std::ostream&运算符?我当前的实现不起作用://MyTest.htemplateclassMyTest{inlinefriendstd::ostream&operator(std::ostream&lhs,constMyTest&rhs);};//MyTest.cpptemplateinlinefriendstd::ostream&operator(std::ostream&lhs,constMyTest&rhs){//IMPLEMENTATION}非常感谢! 最佳答案 引用op

c++ - 重载左移运算符

我研究发现,当你想为cout重载输出流运算符时,正确的做法是这样做:std::ostream&operator这个函数必须在类之外定义,因为这里发生的是operator特定类的重载应该如下所示:std::ostream&operator编译器/库如何为第二个参数采用泛型定义,特别是因为在C++中没有泛型类(例如Java中的Object)这样的东西? 最佳答案 我觉得你在这里很困惑:theoperator一个是真的operator在classstd::ostream中定义.其实里面定义了好几个版本。但这些与我们无关。此外,它们不是fr

c++ - std::ostream 第一次使用时未正确格式化 const char*

我一直在编写自定义std::streambuf作为日志系统的一部分。但是,我遇到了流输出的第一段格式不正确的问题。这是一个不使用任何自定义streambuf或ostream类的简化测试用例:#includeintmain(){std::streambuf*coutbuf=std::cout.rdbuf();std::ostream(coutbuf)使用g++编译:$g++--versiong++(Ubuntu4.4.1-4ubuntu8)4.4.1$g++-ofailreduced-case.cpp$./fail0x400c80:writingtocoutusingaseparateo

c++ - 命名空间 + 重载 std::ostream << 运算符

我正在尝试在我的C++应用程序中创建一个Vector3D类。对于我的整个程序,我使用的是命名空间。在这个命名空间中,我声明了我的Vector3D类和一个重载的运算符namespacespace{classVector3D{public:floatx,y,z;Vector3D(float_x=0,float_y=0,float_z=0);Vector3D(constVector3D&_vector);Vector3D&operator=(constVector3D&_vector);Vector3Doperator*(float_scalar);Vector3Doperator*(con

c++ - 尝试构建 muParser:错误:'std::basic_ostream 的显式实例化但没有可用的定义

我试图在mac上构建muParser,它一直有效,直到我将XCode升级到4.4并更新了gcc。现在我得到以下代码行生成我不明白的错误:mu::console()&std::operator&,conststd::basic_string&)[with_CharT=char,_Traits=std::char_traits,_Alloc=std::allocator]'butnodefinitionavailable../muparser/src/muParserBase.cpp:Ininstantiationof'std::basic_ostream&std::operator&,c

c++ - 从 std::ostream_iterator 调用时未找到运算符 << 的重载?

这个程序//main.cpp#include#include#include#include#includetemplatestd::ostream&operator&pair){returnos";}intmain(){std::mapmap={{1,2},{2,3}};std::cout>(std::cout,""));//thisdoesn'twork}产生错误nomatchfor‘operator>::ostream_type{akastd::basic_ostream}’and‘conststd::pair’)我猜这是行不通的,因为我的重载在std::copy中不可用,但这是

c++ - 为什么我可以 std::move 流右值引用到左值引用?

据我了解C++11引用,我不应该能够将右值引用绑定(bind)到(非常量)左值引用,因为前者可能绑定(bind)到临时对象,而后者绝不能绑定(bind)到一个临时的。但是我发现这种奇怪的行为与临时流对象有关(我尽可能地减少了)structDummy{};templateStream&operatorvoidpass(Stream&&s){std::move(s)lvalueconversion?}#includeintmain(){pass(std::fstream("test",std::ios::out));}如果我写s在线(X),C++在(A)行提示,说error:invalid

c++ - 二进制表达式 ('ostream'(又名 'basic_ostream<char>')和 'ostream' 的无效操作数)

我正在努力cout但是,编译时出现“二进制表达式的无效操作数('ostream'(又名'basic_ostream')和'ostream')”错误。#includeusingnamespacestd;ostream&Print(ostream&out){out为什么这不起作用?我怎样才能解决这个问题?谢谢!! 最佳答案 您可能正在寻找的语法是std::cout.pointer函数被视为操纵器。内置operator将指针指向Print并用cout调用它.#includeusingnamespacestd;ostream&Print(o

c# - c++ - 使用 std 命名空间和依赖项

在尝试熟悉C++及其概念时,我遇到了usingnamespacestd和#include我的简单代码如下#include"stdafx.h"#include"ConsoleApplication5.h"#includeintmain(){std::cout使用使用intellisense的VisualStudio2015Community显示cout使用以下内容std::ostreamstd::cout作为一名C#程序员,这让我有些困惑。这是:std::ostream作为返回类型而std::cout是传递的方法/参数还是std::ostream依赖于cout更新(在Archimared

c++ - 为什么 ranges::ostream_iterator 是默认可构造的?

这个问题遵循评论中的讨论here.在EricNiebler的ranges-v3library中(这有点成为C++20标准的一部分),ranges::ostream_iterator是default-constructible-没有ostream。怎么会?我认为后来有效构造的“虚拟”构造是C++中的反模式,我们正在逐渐摆脱这种缺陷。std::ostream迭代器canonlybeconstructedwithastream(目前-在C++20之前)。似乎我们可以用默认构造的range::ostream_iterator做任何事情...所以,这是怎么回事? 最佳