【问题标题】:Unable to output vector with an ostream output C++无法使用 ostream 输出 C++ 输出向量
【发布时间】:2017-05-16 12:19:28
【问题描述】:

您好,我想知道是否可以帮助我解决在 C++ 中打印出向量内容的问题

我试图在一个或两个函数调用中以特定顺序输出一个类的所有变量。但是我在遍历向量时收到一个奇怪的错误

我收到的错误是错误

 error C2679: binary '=' : no operator found which takes a right-hand operand of type 
'std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>' (or there is no acceptable conversion) 

我的相关代码如下

ifndef idea
#define idea

using namespace std;

class Idea{
private:
    int id;
    string proposer, content;
    vector<string> keywords;
public:
    Idea(int i);
    Idea(int i, string pro, string con);
    void inputIdea();
    int getID(){ return id; };
    string getProposer(){ return proposer; };
    string getContent(){ return content; };
    vector<string> getKeyword();
    bool wordSearch(string word);
    friend ostream& operator<< (ostream& stream, const Idea& i);
    void printIdea(ostream& os)const;
};

bool Idea::wordSearch(string word){
     vector<string>::iterator it;
     for(it = keywords.begin(); it < keywords.end(); it++){
         if (word == *it){
             return true;
         }
     }

     if (content.find(word) != string::npos){
         return true;
     }

     return false;
 }

void Idea::printIdea(ostream& os)const{
     vector<string>::iterator it;

     os << "ID: " << id << endl;
     os << "Proposer:  " << proposer << endl;
     os << "keywords: ";
     for (it = keywords.begin(); it < keywords.end(); it++){ // error C2679
         os << *it << " ,";
     }
     os << endl << "content: " << content << endl;

 }

 ostream& operator<<(ostream& os, const Idea& i)
 {
     i.printIdea(os);
     return os;

 }

我觉得这很奇怪,因为迭代器函数在代码的不同部分工作。

 bool Idea::wordSearch(string word){
     vector<string>::iterator it;
     for(it = keywords.begin(); it < keywords.end(); it++){
         if (word == *it){
             return true;
         }
     }

     if (content.find(word) != string::npos){
         return true;
     }

     return false;
 }

我希望打印出 id,然后是提议者,然后是关键字,然后是内容。

【问题讨论】:

  • 另外,使用!=而不是&lt;来循环使用迭代器,它允许其他迭代器继续工作。
  • 您好,感谢您的回答,经过一个多小时的调查,我实际上能够自己解决错误。我已经从我的重载函数调用中删除了 const 标签。我会记住你的建议,但是学习总是好的。如果它确实对输出有效,将再次回复!

标签: c++ vector operator-overloading ostream


【解决方案1】:

这是因为printIdea 是一个const 方法。 const 方法不允许修改其成员,因此 keywords.begin() 返回一个 vector&lt;string&gt;::const_iterator,而不是正常的 (vector&lt;string&gt;::iterator) 。你应该改变it的类型:

vector<string>::iterator it;

到:

vector<string>::const_iterator it;

拥有兼容的类型。

或者,如果您有一个支持 C++11 的编译器,您可以让编译器为您解决:

auto it = keywords.begin()

【讨论】:

    【解决方案2】:

    printIdea 定义为

    void Idea::printIdea(ostream& os)const
    

    这意味着该函数中的所有非静态成员都是 const 限定的。因为keywords.begin() 返回std::vector::const_iterator 而不是std::vector::iteratorstd::vector::const_iterator 不能分配给 ``std::vector::iteratorwhich is howit` 被声明所以你得到错误。你需要改变

    vector<string>::iterator it;
    

    vector<string>::const_iterator it;
    

    为了让它工作。

    或者,您可以使用基于范围的 for 循环,甚至不必记住所有这些内容

    for (auto& e : keywords)
    {
         os << e << " ,";
    }
    

    【讨论】:

      【解决方案3】:

      您需要使用const_iterator,因为您的方法printIdea() 是const。

      您应该使用 != 将迭代器与 end() 进行比较。

      【讨论】:

        猜你喜欢
        • 2019-04-20
        • 1970-01-01
        • 2018-10-04
        • 1970-01-01
        • 1970-01-01
        • 2018-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多