我有以下课程:
class Friend
{
public:
Friend();
~Friend(){}
void setName(string friendName){ name = friendName; }
void setAge(int friendAge) { age = friendAge; }
void setHeight(int friendHeight) { height = friendHeight; }
void printFriendInfo();
private:
string name;
int age;
float height;
};
//implementations
Friend::Friend()
{
age = 0;
height = 0.0;
}
//printing
void Friend::printFriendInfo()
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Height : " << height << endl << endl;
}
此时我可以在矢量中引入值,如下所示:
std::vector<Friend> regist(4, Friend());
regist[1].setAge(15);
regist[1].setHeight(90);
regist[1].setName("eieiei");
regist[2].setAge(40);
regist[2].setHeight(85);
regist[2].setName("random");
在调试中,此解决方案正常工作。但现在我正在尝试打印矢量。到目前为止没有成功。
for (int i = 0; i < regist.size(); i++) {
cout << regist[i]; //<-- error here
cout << '\n';
}
答案 0 :(得分:1)
只需致电 $client = new Services_Twilio($AccountSid, $AuthToken);
foreach ($client->account->messages->getIterator(0, 50, array()) as $message) {
foreach ($message->media as $media) {
echo "http://api.twilio.com" . $media->uri;
}
}
会员功能:
printFriendInfo()
答案 1 :(得分:1)
有关
cout << regist[i];
开始工作,在Friend
string getName() const { return name; }
int getAge() const { return age; }
float getHeight() const { return height; }
并实现一个重载的operator<<
函数:
std::ostream& operator<<(std::ostream& out, Friend const& f)
{
out << "Name : " << f.getName() << std::endl;
out << "Age : " << f.getAge() << std::endl;
out << "Height : " << f.getHeight() << std::endl;
return out;
}
答案 2 :(得分:1)
您可能会重新设计一点(实质上):
#include <iostream>
class Friend
{
public:
Friend();
// A more general name, const, and taking a stream.
void write(std::ostream&) const;
private:
std::string name;
int age;
float height;
};
Friend::Friend()
{
age = 0;
height = 0.0;
}
void Friend::write(std::ostream& stream) const
{
stream << "Name : " << name << std::endl;
stream << "Age : " << age << std::endl;
stream << "Height : " << height << std::endl << std::endl;
}
// Forward to the member function
inline std::ostream& operator << (std::ostream& stream, const Friend& object) {
object.write(stream);
return stream;
}
int main() {
Friend f;
std::cout << f;
}