【问题标题】:VS2015 compiler error C2679:VS2015编译错误C2679:
【发布时间】:2015-10-25 07:24:17
【问题描述】:

我在尝试使用std::ostream 打印时遇到问题,我检查了代码两次和三次。但无济于事。 VC14 总是返回如下错误:

error C2679: binary '

如果有人请帮我弄清楚这段代码有什么问题,我想知道

代码:

#include <iostream>
#include <iomanip>
#include <array>
#include <cstring>

struct Student
{
    std::string name;
    int midTerm;
    char grade;

    static void displayHeader(std::ostream& str)
    {
        str << std::left
            << std::setw(10) << " Names"
            << std::setw(10) << "Exam"
            << std::setw(10) << "Grade";
        str << std::setfill('-') << std::setw(28) << '\n';
    }

    friend std::ostream& operator<<(std::ostream& str, const Student& data)
    {
        str << std::setfill(' ') << std::left
            << std::setw(10) << data.name
            << std::setw(11) << data.midTerm
            << std::setw(2) << data.grade;

        return str;
    }
};

template<typename C>
void display(const C& c)
{
    using ValueType = typename C::value_type;

    ValueType::displayHeader(std::cout);

    for (const auto& i : c)
    {
        std::cout << '\n' << i;
    }
}

int main()
{
    std::array<Student, 5> a
    {
        {
            { "me",      60, 'D' },
            { "matt",    88, 'B' },
            { "pop",     88, 'B' },
            { "john",    93, 'A' },
            { "jesseca", 82, 'B' }

        }
    };

    display(a);

    std::sort(a.begin(), a.end(),
        [](const auto& a, const auto& b)
    {
        return std::tie(a.midTerm, a.grade, a.name) > std::tie(b.midTerm, b.grade, b.name);
    });

    std::cout << "\n\nafter sorting\n\n";

    display(a);
}

【问题讨论】:

  • 您缺少一些包括,例如&lt;tuple&gt;&lt;string&gt;&lt;algorithm&gt;
  • @PiotrSkotnicki 谢谢,我包括了`,我不知道这是怎么发生的,但是谢谢

标签: c++ c++11 visual-studio-2015


【解决方案1】:

正如 Piotr 所指出的,问题在于您忘记包含 &lt;string&gt;

您没有收到关于未知类型std::string 的明确警告的原因是视觉工作室vector 包括stdexcept,其中包括xstring,其中包含std::string 的前向声明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2017-01-26
    • 1970-01-01
    • 2016-08-25
    相关资源
    最近更新 更多