【发布时间】:2019-08-10 05:22:32
【问题描述】:
我是 C++ 的 4 小时新手,并且已经用字符串向量碰了壁。尝试将多个字符串添加到字符串向量时,我不断出错。我想使用push_back。
我也想显示这个字符串向量,但我不确定如何(我知道如何显示非向量)。鉴于我无法将字符串添加到字符串向量,我还没有尝试显示字符串向量。
profile.hpp
#include <iostream>
#include <vector>
class Profile
{
private:
std::string name;
std::string city;
std::string country;
int age;
std::vector<std::string> hobbies;
public:
std::vector<std::string> add_hobbies(std::string new_hobbies);
};
profile.cpp
#include <iostream>
#include "profile.hpp"
Profile::Profile(std::string new_name, int new_age, std::string new_city, std::string new_country)
: name(new_name), age(new_age), city(new_city), country(new_country)
{}
void Profile::add_hobbies(std::string new_hobbies)
{
hobbies.push_back(new_hobbies);
}
app.cpp
#include <iostream>
#include "profile.hpp"
int main()
{
Profile sam("Sam Drakkila", 30, "New York", "USA");
sam.add_hobbies("Play golf", "Read books", "Eat tennis balls"); // This doesn't seem to work.
}
g++ app.cpp profile.cpp。打印大量错误日志。
【问题讨论】:
-
为了显示字符串的向量,你可以用谷歌搜索它:How to print out the contents of a vector?
标签: c++ class c++11 stdvector stdstring