重载<<像cout错误

时间:2012-01-05 22:31:50

标签: c++ operator-overloading

我正在尝试使用彩色输出创建类似 std :: cout 的类。我的想法是调用colorstream,但当我重载运算符<<给出错误。

以下代码:

的main.cpp

#include <colorstream/colorstream.hpp>

int main ( int argc, char **argv )
{
cpk::colorstream test;
test << "Hello World";

return 0;
}

colorstream / colorstream.hpp

#include <string>

#ifndef CPK_COLORSTREAM_HPP
#define CPK_COLORSTREAM_HPP

namespace cpk
{
    class colorstream
    {
    public:
        colorstream ( ) { };
        colorstream operator<<( std::string n );
    };
}

#endif // #ifndef CPK_COLORSTREAM_HPP

colorstream / colorstream.cpp

#include <string>
#include <iostream>

/**
 * CPK Color Stream Header
 */
#include <colorstream/colorstream.hpp>

cpk::colorstream::colorstream operator<<( std::string n )
{
    std::cout << n << std::endl;
}

这是我第一次尝试重载操作员,所以请帮助我,如果我能解释我的错误。

谢谢你,Bruno Alano

@edit 错误:

CMakeFiles/cpk.dir/source/cpk.cpp.o: In function `main':
cpk.cpp:(.text+0x45): undefined reference to `cpk::colorstream::operator<<(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make[2]: ** [cpk] Erro 1
make[1]: ** [CMakeFiles/cpk.dir/all] Erro 2
make: ** [all] Erro 2

3 个答案:

答案 0 :(得分:4)

嗯,错误是你对运营商的定义是乱码。它应该是

cpk :: colorstream cpk :: colorstream :: operator&lt;&lt; (std :: string n)

那就是说,我强烈建议进一步采用这种方法!要创建您想要从std::streambuf派生的自定义流,并覆盖其中的相关操作(例如overflow(),可能还有xsputn()用于输出流。

实际上,如果你想改变颜色,例如使用ANSI Escape Codes,您只需创建合适的颜色操纵器,并将其与std::ostream

一起使用
#include <iostream>

namespace color
{
    std::ostream& red(std::ostream& out) {
        return out << "\x1b[31m";
    }
    std::ostream& reset(std::ostream& out) {
        return out << "\x1b[0m";
    }
}

int main()
{
    std::cout << "hello " << color::red << "world" << color::reset << "\n";
}

在非UNIX平台上,可能还需要一些其他机制,但是......

答案 1 :(得分:2)

定义成员运算符时,需要使用类名限定的是运算符名称,而不是返回类型。

namespace cpk {
    colorstream colorstream::operator<<( std::string n )
    {
        std::cout << n << std::endl;
    }
}

现在,要允许将<<的来电链接到std::cout,就像使用namespace cpk { colorstream colorstream::operator<<( std::string n ) { std::cout << n << std::endl; return *this; } } 一样,您需要从运营商那里实际返回一个值!

    colorstream& colorstream::operator<<( std::string n )
    {
        std::cout << n << std::endl;
        return *this;
    }

但是,这将返回流的副本,因为它按值返回。这可能是不可取的,所以通过引用返回可能会更好:

{{1}}

答案 2 :(得分:0)

我需要更多关于你要完成什么以及将要出现的错误的信息。

我使用下面提供的代码假设您要将颜色输出到标准输出。 我觉得我对这个假设完全错了,不过这是代码......

<强>的main.cpp

#include <colorstream/colorstream.hpp>

int main ( int argc, char **argv )
{
    cpk::colorstream test;
    test.SetColor("RED");
    std::cout << test << std::endl;

    return 0;
}

colorstream / colorstream.hpp 应为:

#include <iostream>
#include <string>

#ifndef CPK_COLORSTREAM_HPP
#define CPK_COLORSTREAM_HPP

namespace cpk
{
    class colorstream
    {
    public:
        std::ostream& operator<< (std::ostream& stream, const cpk::colorstream& cs);
        void SetColor(const std::string &color){m_Color = color;}
    private:
        std::string m_Color;
    };
}

#endif // #ifndef CPK_COLORSTREAM_HPP

colorstream / colorstream.cpp 应为:

#include <string>
#include <iostream>

/**
 * CPK Color Stream Header
 */
#include <colorstream/colorstream.hpp>

std::ostream& operator<<(std::ostream& stream, const cpk::colorstream& cs) 
{
    stream << cs.m_Color; 
    return stream;
}