【问题标题】:C++ output vector contents that is an object type对象类型的 C++ 输出向量内容
【发布时间】:2020-12-01 22:38:42
【问题描述】:

在我的主要.cpp 文件中,我有一个包含Band 类型元素的向量。 Band 是我的 implementation.cpp 文件中的结构的名称。我的主文件如下所示:

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

这行代码我有一个对应的.h文件:

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

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

在我的主文件中,我尝试使用以下增强的for 循环来打印矢量内容:

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

但是,我在使用第一组 &lt;&lt; 运算符时遇到错误:

没有操作符“

如何打印出我的bandsVec 向量的内容?

【问题讨论】:

    标签: c++ object vector struct


    【解决方案1】:

    您需要定义一个重载运算符std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Band&amp;); C++ 不知道如何自动打印任何旧结构。比如

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

    如果您知道如何解释它,那么您收到的错误消息就会准确地告诉您问题出在哪里。

    【讨论】:

      【解决方案2】:

      std::vector 与输出

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

      【讨论】:

        【解决方案3】:

        您还没有提供将结构序列化到流中的方法。 您必须提供 operator>> 。它可以是成员函数和单独的运算符。它很少作为成员函数提供,原因在下面的示例中解释。

        如果您希望将所有内容都保留在类中,则可以使用朋友。从技术上讲,它将是一个单独的运算符,但您将代码放在您的类中。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-15
          • 1970-01-01
          • 2013-01-13
          • 1970-01-01
          • 1970-01-01
          • 2021-05-05
          • 2010-12-20
          • 2012-06-04
          相关资源
          最近更新 更多