【问题标题】:c++ print a vector that contain a structc++ 打印一个包含结构的向量
【发布时间】:2014-01-05 03:33:09
【问题描述】:

我是 C++ 初学者。谁能告诉我如何打印名为 mailVector 的矢量。这是我的代码:

#include<iostream>
#include<vector>
using namespace std;
struct eMailMsg {
string to; // i.e. "professor@stanford.edu"
string from; // i.e. "student@stanford.edu"
string message; // body of message
string subject; // i.e. "CS106 Rocks!"
int date; // date email was sent
int time; // time email was sent
};

int main(){
    vector <eMailMsg> mailVector;
    eMailMsg professor={"professor@stanford.edu","student@stanford.edu","body of message","CS106 Rocks",4,16};
    mailVector.push_back(professor);
    for( std::vector<eMailMsg>::const_iterator i = mailVector.begin(); i != mailVector.end(); ++i)
    std::cout << *i << ' ';

    return 0;
}

相应的错误是错误 1 ​​错误 C2679: binary '

更新版本 1 :

#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
struct eMailMsg {
string to; // i.e. "professor@stanford.edu"
string from; // i.e. "student@stanford.edu"
string message; // body of message
string subject; // i.e. "CS106 Rocks!"
int date; // date email was sent
int time; // time email was sent
};

ostream& operator<<(ostream& os, const eMailMsg& rightOp)
    {
   os << rightOp.to << " " << rightOp.from << "etc ...";//error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)

   return os;
   // We're writing std::string here and C++ can do that
}

int main(){
    vector <eMailMsg> mailVector;
    eMailMsg professor={"professor@stanford.edu","student@stanford.edu","body of message","CS106 Rocks",4,16};
    mailVector.push_back(professor);

    for( std::vector<eMailMsg>::const_iterator i = mailVector.begin(); i != mailVector.end(); ++i)
std::cout << *i << ' ';

    return 0;
}

【问题讨论】:

标签: c++ vector struct


【解决方案1】:

您正在尝试打印eMailMsg 类型。 C++ 不知道怎么做,你需要告诉它。

重载ostream&amp; operator&lt;&lt;(ostream&amp; os, const eMailMsg&amp; rightOp) 以教它。

你可以这样做:

... {
   os << rightOp.to << " " << rightOp.from << "etc ...";
   return os;
   // We're writing std::string here and C++ can do that
}

【讨论】:

  • @BenSeedangie 你想告诉我们什么?
  • @BenSeedangie Ehhh,那个重载(运算符
  • @ScarletAmaranth 是的,你抓住了我。哈哈。但是我怎么能在 main 中使用这个函数呢?只是cout&lt;&lt;mailVector.to&lt;&lt;endl??
  • 在你的情况下只是cout &lt;&lt; *t,“eMailMsg,它会神奇地调用它正在寻找但找不到的operator&lt;&lt;(ostream, eMailMsg)原来,现在有了
  • @ScarletAmaranth 好的。我知道你在说什么。太感谢了。但是,os&lt;&lt;rightOp.to 有一个错误,请查看我更新的代码。错误 C2679: 二进制 '
【解决方案2】:

试试这个:

#include<iostream>
#include<vector>
using namespace std;
struct eMailMsg {
string to; // i.e. "professor@stanford.edu"
string from; // i.e. "student@stanford.edu"
string message; // body of message
string subject; // i.e. "CS106 Rocks!"
int date; // date email was sent
int time; // time email was sent

    operator char const * () const
    {
        return  to.c_str();
    }

};

int main(){
    vector <eMailMsg> mailVector;
    eMailMsg professor={"professor@stanford.edu","student@stanford.edu","body of message","CS106 Rocks",4,16};
    mailVector.push_back(professor);
    for( std::vector<eMailMsg>::const_iterator i = mailVector.begin(); i != mailVector.end(); ++i)
    std::cout << *i << ' ';

    return 0;
}

请注意 cast 运算符方法格式化结构,c++ 编译器决定使用该运算符强制转换为“char *”,因为这是 outstream 的“

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2011-08-07
    • 1970-01-01
    • 2018-09-26
    相关资源
    最近更新 更多