Adam Wong
Adam Wong

Reputation: 9

C++ output vector contents that is an object type

Within my main .cpp file, I have a vector holding elements of type Band. Band is the name of a struct in my implementation.cpp file. My main file is shown below:

int main(int argc,char* argv[]){
    std::vector<Band> bandsVec = readbandFile(argv[1]);
}

I have a corresponding .h file for this line of code:

struct Band {
    std::string bandName;
    std::string listofMembers;
};

std::vector<Band> readbandFile(std::string a);

In my main file, I attempted to use the following enchanced for loop to print the vector contents:

for (Band band: bandsVec) {
    std::cout << band << " ";
}

However, I get an error on the first set of << operators being used:

no operator "<<" matches these operands -- operand types are: std::ostream << Band

How can I print out the contents of my bandsVec vector?

Upvotes: 0

Views: 79

Answers (3)

Vasilij
Vasilij

Reputation: 1941

You haven't provided the method to serialize your struct into the stream. You have to provide the operator>> . It can be a member function and a separate operator. It is very rarely provided as a member function, the reason is explained in the example below.

If you prefer to keep everything inside the class you can use friend. Technically it will be a separate operator, but you place the code inside your class.

#include <cstring>
#include <iostream>
#include <string>

// member function
class Band1 {

    std::string name{"name"};
    std::string members{"members"};

public:
    // this (pointer to the Band itself) is implicitly passed as the first parameter and the stream is the second
    // note the difference with non-member function where the stream is the first and reference to Band is the second parameter
    // this results in a very weird call (see example in main)
    std::ostream& operator<<(std::ostream& os) {
        return os << name << members;
    }
};

// operator
struct Band2 {
    std::string name{"name"};
    std::string members{"members"};
};

std::ostream& operator<<(std::ostream& os, const Band2& band) {
    return os << band.name << band.members;
}

// friend operator
class Band3 {

    // note the friend operator is in private section, but this is not a member function
    friend std::ostream& operator<<(std::ostream& os, const Band3& band) {
        return os << band.name << band.members;
    }

    std::string name{"name"};
    std::string members{"members"};
};

int main(int argc, char *argv[])
{
    Band1{} << std::cout << std::endl; // very confusing call in case of member function
    std::cout << Band2{} << std::endl;
    std::cout << Band3{} << std::endl;

    return 0;
}

Upvotes: 0

John Park
John Park

Reputation: 1784

std::vector has nothing to do with output << operator. You could write your own code to print each of the element.

for(const auto& band : bandsVec)
{
    std::cout<<band.bandName<<" "<<band.listofMembers<<std::endl;
}

Upvotes: 0

john
john

Reputation: 88092

You need to define an overloaded operator std::ostream& operator<<(std::ostream&, const Band&); C++ doesn't know how to print any old struct automatically. For instance

std::ostream& operator<<(std::ostream& out, const Band& b)
{
    return out << b.bandName << ' ' << b.listOfMembers;
}

If you know how to interpret it, the error message you got is telling you exactly what the problem is.

Upvotes: 5

Related Questions