MORTAL
MORTAL

Reputation: 383

VS2015 compiler error C2679:

i'm having a problem when trying to print out with std::ostream i have checked the code twice and triple. but no avail. VC14 always gives error back like below:

error C2679: binary '<<': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)

i would like if someone please help me to fiugre out what wrong with this code

code:

#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);
}

Upvotes: 0

Views: 598

Answers (1)

Johan Lundberg
Johan Lundberg

Reputation: 27078

As Piotr points out, the problem is that you forgot to include <string>.

The reason you don't get an explicit warning about unknown type std::string it is that the visual studios vector includes stdexcept which includes xstring which contains a forward declaration of std::string.

Upvotes: 1

Related Questions