4 c++ class stdstring stdvector c++11
I am 4 hours-new to C++ and have hit a brick wall with string vectors. When trying to add multiple strings to a string vector, I keep erroring out. I'd like to use push_back.
I would also like to display this string vector, but I'm not sure how (I know how to display non-vectors). Given that I have not been able to add a string to a vector of strings, I did not attempt to display the vector of strings yet.
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);
};
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
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.
}
Run Code Online (Sandbox Code Playgroud)
g++ app.cpp profile.cpp. Prints a massive log of errors.
You have the following problems in your code:
add_hobbies returns std::vector<std::string>,
but in definition you have returned void. Presumably, you should have declared as a void function as it seems to be a setter function.Secondly, you are passing a number of strings instead of a single string which you defined here:
void Profile::add_hobbies(std::string new_hobbies) //>>> as per defenition, only one string can be passed!
// ^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
如果要传递任意数量的字符串,可以std::initializer_list<std::string>改用。
第三,您缺少头文件中的构造函数声明。在类的定义中添加profile.hpp
Profile(std::string new_name, int new_age, std::string new_city, std::string new_country);
Run Code Online (Sandbox Code Playgroud)最后但并非最不重要的一点是,您需要包含<string>标题才能使用std::string(credits @????? ??? )
这意味着,(查看在线直播)
#include <iostream>
#include <vector>
#include <string> // std::string
#include <initializer_list> // std::initializer_list
class Profile
{
private:
// ... other members
std::vector<std::string> hobbies;
public:
// ... other member functions
void add_hobbies(std::initializer_list<std::string> new_hobbies)
//^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{
hobbies.reserve(hobbies.size() + new_hobbies.size()); // reserve memory to avoid, unwanted reallocations
for (const auto& hobby : new_hobbies) hobbies.emplace_back(hobby);
}
};
int main()
{
Profile sam{};
sam.add_hobbies({ "Play golf", "Read books", "Eat tennis balls" }); // now works
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
Run Code Online (Sandbox Code Playgroud)
或者使用可变参数模板和c++17特征折叠表达式。 (见在线直播)
#include <iostream>
#include <vector>
#include <string> // std::string
#include <type_traits> // std::enable_if, std::is_same, std::common_type
using namespace std::literals;
class Profile
{
private:
// ... other members
std::vector<std::string> hobbies;
public:
// ... other member functions
template<typename... Args> // sfinae to restrict the Args type to be only std::string s
std::enable_if_t<std::is_same_v<std::common_type_t<Args...>, std::string>>
add_hobbies(Args&& ... args)
{
hobbies.reserve(hobbies.size() + sizeof...(Args));
(hobbies.emplace_back(std::forward<Args>(args)), ...);
}
};
int main()
{
Profile sam{};
sam.add_hobbies("Play golf"s, "Read books"s, "Eat tennis balls"s); // now works
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
224 次 |
| 最近记录: |