相关疑难解决方法(0)

将std :: endl传递给std :: operator <<

在这个Stack Overflow回答中, 它表示与之std::cout << "Hello World!" << std::endl;相同

std::operator<<(std::operator<<(std::cout, "Hello World!"), std::endl);
Run Code Online (Sandbox Code Playgroud)

但是当我编译上面的代码行时,它不会编译!然后尝试别的东西后,我发现,它不会编译的原因是因为std::endl,如果我取代std::endl"\n",然后它工作.但为什么你不能传递std::endlstd::operator<<

或者更简单,是不是std::cout<<std::endl;一样std::operator<<(std::cout, std::endl);

编辑

编译时icpc test.cpp,错误消息是 error: no instance of overloaded function "std::operator<<" matches the argument list argument types are: (std::ostream, <unknown-type>) std::operator<<(std::cout, std::endl);

g++ test.cpp提供更长的错误消息.

c++ std

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

STL中的自由功能需要名称空间吗?

我正在玩我的编码风格.我曾经明确地为每个库调用添加前缀,std::但我转而使用这样的声明:

using std::count;
using std::vector;
Run Code Online (Sandbox Code Playgroud)

我在过去几天注意到的一件事是,有时如果我忘记了使用声明 - 这using std::vector;是一个很好的例子 - 我会得到大量的编译器错误.但是,如果我忽略了命名空间delcare,那么像using std::count;我的代码编译算法就好了.

这是否与类和自由函数的区别有关?在所有参考部位,两者count(first, last, value)vector带有前缀std::,所以我希望他们有同样的表现.

或者它与全局命名空间中的其他函数有关吗?我注意到std::max似乎也需要一个名称空间声明,也许它在默认包含的Apple/glibc/LLVM文件中定义,因此如果我使用它没有名称空间声明会有冲突吗?

我正在使用Apple LLVM 7.0.2.在El Capitan.

编辑:向我们展示代码

#include <algorithm>
#include <vector>

using std::count;
using std::vector;

int main() {
    vector<int> v = { 1, 2, 3, 4 };
    return count(begin(v), end(v), 3);
}
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11

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

为什么 gtest 看不到 == 的定义?

我有一个模板类Matrix

\n\n
    template<typename T>\n    class Matrix {\n        //blah-blah-blah\n        }\n
Run Code Online (Sandbox Code Playgroud)\n\n

以及以下运算符:

\n\n
template<typename T>\nbool operator==(const Matrixes::Matrix<T> &lhs, const Matrixes::Matrix<T> &rhs) {\n    if (lhs.get_cols()!=rhs.get_cols() || lhs.get_rows()!=rhs.get_rows()){\n        return false;\n    }\n    for (int r = 0; r < lhs.get_rows(); ++r) {\n        for (int c = 0; c < lhs.get_cols(); ++c) {\n            if (lhs.get(r, c) != rhs.get(r, c)) {\n                return false;\n            }\n        }\n\n    }\n    return true;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

上述运算符是在Matrixes命名空间之外定义的。

\n\n

我有一些测试(我正在使用框架 Google Tests)。但是,如果我写这样的东西:

\n\n
TEST(MatrixOperations, MatrixMultiplicationSimple) {\n    Matrixes::Primitives<int>::VectorMatrix vec1{{{8, 3, 5, …
Run Code Online (Sandbox Code Playgroud)

c++ templates googletest

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

为什么`std::ostream&amp; operator&lt;&lt;` 覆盖必须在 C++ 的“全局”范围内声明?

描述

我重写<< operatorstd::ostream缓解对象显示在我的代码。我使用不同的命名空间来定义要显示的对象类型。

这导致我出现编译错误。我找到了解决办法。似乎必须在全局范围内声明覆盖,但我真的不明白为什么。有人可以解释导致错误的原因吗?

错误

main.cpp:22:38: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘std::vector’)
         std::cout <<"printVector = " << data << std::endl;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
Run Code Online (Sandbox Code Playgroud)

示例代码(不编译)

这是一个显示错误的虚拟示例。

#include <iostream>
#include <vector>

inline std::ostream&
operator<<(std::ostream& strm, std::vector<uint8_t>& buffer)
{
    return strm << "display std::vector<uint8_t>";
}
    
namespace aNamespace {
    enum TestEnum { a, b, c };
    
    inline std::ostream&
    operator<<(std::ostream& strm, TestEnum& value)
    {
        return strm << "display TestEnum";
    }
  
    static void printVector () 
    {
        std::vector<uint8_t> data {1, …
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么结构必须与模板类位于同一名称空间中才能编译?

问题的标题并没有透露太多关于我的问题,但我试图用一个短语来解释这个问题.这是问题,我在Windows中使用MinGW和Linux中使用GCC编译的应用程序中有类似的代码结构.Visual Studio不会出现任何问题.结构如下:

#include <iostream>

namespace idb
{
    class Wrapper
    {
    public:
        template<typename S>
        void boo(S& s)
        {
            bind(s);
        }
    };
}

namespace idb // <- if this namespace changes, code explodes
{
    struct Fulalas
    {
        int x;
    };
}

namespace idb
{
    void bind(idb::Fulalas f)
    {
        std::cout << f.x << std::endl;
    }
}

namespace app
{
    class Foo
    {
    public:
        void func()
        {
            idb::Fulalas f;
            f.x = 5;
            w.boo(f);
        }

    private:
        idb::Wrapper w;
    };
}

int main()
{
    app::Foo f; …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates mingw

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

有没有理由find_if,for_each,count等不需要std ::?

我刚刚发现标准algorithm头中的几种算法不需要std::.

例:

#include <vector>
#include <algorithm>

int main() {
  std::vector<int> m;
  count(m.begin(), m.end(), 0);
  count_if(m.begin(), m.end(), [](auto){return true;});
  for_each(m.begin(), m.end(), [](auto){});
  find_if(m.begin(), m.end(), [](auto){return true;});
}
Run Code Online (Sandbox Code Playgroud)

在coliru现场演示

这有什么具体原因吗?双方g++clang++接受上述代码.

c++ algorithm std c++11

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

C++ — `algorithm` 库和命名空间 `std`

我发现无论有algorithm没有命名空间都可以使用库的许多(也许是所有)函数std:例如,何时algorithm导入:

#include <algorithm>
Run Code Online (Sandbox Code Playgroud)

std::unique并且unique似乎是等价的。下面是一个例子:

#include <iostream>
#include <vector>
#include <algorithm>


int main () {
    std::vector<int> v = {10,20,20,20,30,30,20,20,10};
    std::vector<int>::iterator it;

    it = std::unique (v.begin(), v.end());
    for (it=v.begin(); it!=v.end(); ++it)
        std::cout << ' ' << *it;
        std::cout << '\n';

    it = unique (v.begin(), v.end());
    for (it=v.begin(); it!=v.end(); ++it)
        std::cout << ' ' << *it;
        std::cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)

输出:

10 20 30 20 10 30 20 20 10
10 20 30 20 …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces

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

重载&lt;&lt;运算符导致其模棱两可

因此,对于最近尝试重载<<运算符时遇到的错误,我有一个疑问。

我有一个名为“ structPixels.h”的文件,在其中我定义了一个结构,如下所示:

#pragma once
#include <iostream>

namespace Eng
{
    /**
     * A structure to represent pixels
     */
    typedef struct Pixel{
        unsigned int x; ///The x-coordinate
        unsigned int y; ///The y-coordinate

        bool operator ==(const Pixel& rhs) const
        {
            return (this->x == rhs.x && this->y == rhs.y);
        };

        bool operator !=(const Pixel& rhs) const
        {
            return (this->x != rhs.x || this->y != rhs.y);
        };

        bool operator <(const Pixel& rhs) const
        {
            return std::tie(this->x, this->y) < std::tie(rhs.x, rhs.y);
        }

        friend std::ostream& operator …
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么在对数组进行排序时要强调std?

为什么我们在通过 begin 和 end 迭代器对数组进行排序时必须放置 std,而向量、列表等情况并非如此?

例子:

std::sort( std::begin(array), std::end(array), [](int a,int b) { return a>b;} );
Run Code Online (Sandbox Code Playgroud)

c++ arrays sorting

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

标签 统计

c++ ×9

c++11 ×2

std ×2

templates ×2

algorithm ×1

arrays ×1

gcc ×1

googletest ×1

mingw ×1

namespaces ×1

sorting ×1

stl ×1