相关疑难解决方法(0)

反正有没有把以下内容写成C++宏?

my_macro << 1 << "hello world" << blah->getValue() << std::endl;
Run Code Online (Sandbox Code Playgroud)

应该扩展到:

std::ostringstream oss;
oss << 1 << "hello world" << blah->getValue() << std::endl;
ThreadSafeLogging(oss.str());
Run Code Online (Sandbox Code Playgroud)

c++ logging thread-safety ostringstream

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

通过隐式转换为字符串流式传输对象时,重载决策失败

免责声明:知道应该避免隐式转换为字符串,并且正确的方法是op<<过载Person.


请考虑以下代码:

#include <string>
#include <ostream>
#include <iostream>

struct NameType {
   operator std::string() { return "wobble"; }
};

struct Person {
   NameType name;
};

int main() {
   std::cout << std::string("bobble");
   std::cout << "wibble";

   Person p;
   std::cout << p.name;
}
Run Code Online (Sandbox Code Playgroud)

在GCC 4.3.4上产生以下结果:

prog.cpp: In function ‘int main()’:
prog.cpp:18: error: no match for ‘operator<<’ in ‘std::cout << p.Person::name’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading std implicit-conversion

19
推荐指数
2
解决办法
2135
查看次数

在C++输出流中设置宽度

我试图通过设置不同字段的宽度在C++上创建一个整齐格式的表.我可以使用setw(n),做类似的事情

cout << setw(10) << x << setw(10) << y << endl;
Run Code Online (Sandbox Code Playgroud)

或更改ios_base :: width

cout.width (10);
cout << x;
cout.width (10);
cout << y << endl;
Run Code Online (Sandbox Code Playgroud)

问题是,这两种选择都不允许我设置默认的最小宽度,每次我都要向流写入内容时我必须更改它.

有没有人知道我可以做到这一点而无需无数次重复同一次呼叫?提前致谢.

c++ width

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

C++链接运算符<< for std :: cout like usage

可能重复:
当重载operator << Operator重载时,std :: endl的类型未知

我目前正在编写一个记录器类,但该operator<<方法会导致编译器错误.这是类的最小化版本,在文件"logger.h"中:

#include <iostream>
class Logger {
public:
    Logger() : m_file(std::cout) {}

    template <typename T>
    Logger &operator<<(const T &a) {
        m_file<<a;
        return *this;
    }

protected:
    std::ostream& m_file;
};
Run Code Online (Sandbox Code Playgroud)

它包含在我的main.cpp中,并在输出字符串文字时完美地工作:

log << "hi"; 
Run Code Online (Sandbox Code Playgroud)

但是,以下内容无法编译.

#include "logger.h"
int main() {
    Logger log;

    log << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

g ++编译器报告:

src/main.cpp:5:错误:'log << std :: endl'中'operator <<'不匹配

c++ operators chaining

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

为什么通用引用不捕获函数指针?

在实现简单的记录器时

struct DebugOutput {
    DebugOutput(std::ostream& out = std::cerr) : m_Out(out) {}

    template<typename T>
    inline DebugOutput& operator <<(T&& value) {
        m_Out << value;
        return *this;
    }
private:
    std::ostream& m_Out;
};
Run Code Online (Sandbox Code Playgroud)

我发现std::endl不会被通用引用捕获.

DebugOutput dbg;
dgb << std::endl;
Run Code Online (Sandbox Code Playgroud)

我发现这篇文章解释了你需要在结构中添加一个特别是函数指针签名的重载函数,即:

typedef std::ostream& (*StandardEndLine)(std::ostream&);
inline DebugOutput& operator<<(StandardEndLine manip) {
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

为什么通用引用不捕获函数指针?是不是类型intvoid*

c++ c++11

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

重载operator <<时,C++流作为参数

我正在尝试编写自己的日志记录类并将其用作流:

logger L;
L << "whatever" << std::endl;
Run Code Online (Sandbox Code Playgroud)

这是我开始使用的代码:

#include <iostream>

using namespace std;


class logger{
public:
    template <typename T>
    friend logger& operator <<(logger& log, const T& value);
};

template <typename T>
logger& operator <<(logger& log, T const & value) {
    // Here I'd output the values to a file and stdout, etc.
    cout << value;
    return log;
}

int main(int argc, char *argv[])
{
    logger L;
    L << "hello" << '\n' ; // This works
    L << "bye" << …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading stream endl

10
推荐指数
2
解决办法
3590
查看次数

用于iostream的C++包装器类,使用带有operator <<的std :: endl等流修饰符

我正在为a编写一个包装器std::stringstream,我希望operator<<通过我的类将所有调用转发给std::stringstream.现在这很好用(感谢这个问题:STL流的包装类:前向运算符<<调用),但它仍有一个问题.

假设我有以下代码:

class StreamWrapper {
private:
    std::stringstream buffer;
public:
    template<typename T>
    void write(T &t);

    template<typename T>
    friend StreamWrapper& operator<<(StreamWrapper& o, T const& t);

    // other stuff ...
};


template<typename T>
StreamWrapper& operator<<(StreamWrapper& o, T const& t) {
    o.write(t);
    return o;
}

template<typename T>
void StreamWrapper::write(T& t) {
    // other stuff ...

    buffer << t;

    // other stuff ...
}
Run Code Online (Sandbox Code Playgroud)

如果我现在这样做:

StreamWrapper wrapper;
wrapper << "text" << 15 << "stuff";
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,如果我想使用流修饰符std::endl,这是一个根据 …

c++ iostream stl wrapper

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

ofstream或ostream类型如何将所有类型转换为字符串?

任何系统定义的用户类型过去到ostream对象都转换为字符串或char*?

喜欢cout << 4 <<"Hello World";

工作得非常好,这是如何实现的?是每个类型的<<运算符重载?有没有办法通过一个通用的重载函数实现它?我的意思是我可以只有一个带有一个参数的重载运算符方法(如void*),然后在该方法内部决定如何将整数转换为char*

如果我重载operator <<使用模板ie,事情部分工作

class UIStream
{
private:
 ofstream stream;
public:
 UIStream();
 ~UIStream();
 template <typename T>
 UIStream& operator << (const T);
};
Run Code Online (Sandbox Code Playgroud)

这样可行

 UIStream my_stream;
 my_stream<<"bcd"<10;
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时它会给编译器错误

my_stream <<endl;
Run Code Online (Sandbox Code Playgroud)

错误C2678:二进制'<<':找不到哪个运算符带有'UIStream'类型的左操作数(或者没有可接受的转换)

std :: endl也不是一种对象吗?

c++ operator-overloading operators operator-keyword

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

输出到几个ostreams的类(文件和控制台)

是的,我甚至不确定如何正确地制定这个; 我觉得这是一个涉及的问题.我相信有人可以帮助我.

这就是我想要做的:

  1. 有一个我可以发送东西的课程,就像这样.

    icl << "Blah blah blah" << std::endl;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 我希望能够继承std :: basic_ostream的.attach()类.

  3. 然后,这些类将能够以自己的方式格式化输出.有人可能会添加一个时间戳并写入日志,另一个可能会将其写入控制台,另一个可能会在游戏中显示它.

有人想让我开始朝着正确的方向前进吗?这是我几乎拥有的想法.

#include <vector>

class OutputMan {
    std::vector<std::basic_ostream&> m_Streams;

public:
    void attach(std::basic_ostream& os) {
        m_Streams.push_back(os);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题1:我需要继承和覆盖发送什么

icl << "Blah!" << std::endl;
Run Code Online (Sandbox Code Playgroud)

到m_Streams中的每个流?

问题2:如何继承std :: basic_ostream并创建一个更改输出的类,例如,在其开头添加时间戳?我也希望这个类输出到一个文件.

c++ inheritance ostream

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

为什么编译器会生成错误?

为什么编译器会生成错误?

template<class T>
void ignore (const T &) {}

void f() {
   ignore(std::endl);
}
Run Code Online (Sandbox Code Playgroud)

编译器VS2008给出以下错误:cannot deduce template argument as function argument is ambiguous.

c++ templates stl

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

继承自std :: ostringstream

在我的项目中,我使用一个名为Message的类,它继承自std :: ostringstream,打印出其他类类型的人类可读信息.

所以它的<<运算符多次重载我自己的类类型,我想打印出来.

class Message : public ostringstream
{
public:
    Message() {};
    virtual ~Message() {};

    Message& operator <<(const MyTypeA &a) { return *this << a.getStr(); };
    Message& operator <<(const MyTypeB &b) { return *this << b.identifier(); };
    Message& operator <<(const MyTypeC &c) { return *this << c.foo(); };

    // Pass everything unknown down to ostringstream, and always return a Message&
    template<class T>
    Message& operator <<(const T &t)
    {
        (std::ostringstream&)(*this) << t;
        return *this;
    }
};
Run Code Online (Sandbox Code Playgroud)

没有模板

MyTypeA a,b;
Message …
Run Code Online (Sandbox Code Playgroud)

c++ templates stringstream

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

如何将std :: endl传递给函数并使用它?

我想弄清楚如何将操纵器传递std::endl给函数,然后在函数中使用传入的操纵器.我可以声明这样的函数:

void f(std::ostream&(*pManip)(std::ostream&));
Run Code Online (Sandbox Code Playgroud)

我可以这样称呼它:

f(std::endl);
Run Code Online (Sandbox Code Playgroud)

那一切都很好.我的问题是弄清楚如何在里面使用操纵器f.这不起作用:

void f(std::ostream&(*pManip)(std::ostream&))
{
  std::cout << (*pManip)(std::cout);            // error
}
Run Code Online (Sandbox Code Playgroud)

无论编译器如何,错误消息都归结为编译器无法确定operator<<要调用的内容.我需要修复哪些内容f才能编译代码?

c++ manipulators

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