admin管理员组文章数量:1122832
My code
#include <iostream>
#include <vector>
#include <string>
class Note {
private:
std::string surname;
std::string name;
std::string phone;
std::string birthday;
std::string socials[100];
public:
void print() {
std::cout << name << " " << surname << "\n";
}
Note(std::string _surname, std::string _name, std::string _phone,
std::string _bh, std::string _socials) {
surname = _surname;
name = _name;
phone = _phone;
birthday = _bh;
socials = _socials;
}
std::string getSurname() {
return surname;
}
std::string getName() {
return name;
}
std::string getPhone() {
return phone;
}
std::string getBirthday() {
return birthday;
}
};
int main()
{
//std::string arr[100];
Note nt{ "Andreeva", "Ulyana", "88005553535", "24.04.2004", {"Youtube"}};
std::cout << nt.getBirthday();
}
This string don't work:
socials = _socials;
Idk how to pass the static string (i can't use the vector or dynamic array). I try to create array in main(), and pass to object of Class, this doesn't help too.
My code
#include <iostream>
#include <vector>
#include <string>
class Note {
private:
std::string surname;
std::string name;
std::string phone;
std::string birthday;
std::string socials[100];
public:
void print() {
std::cout << name << " " << surname << "\n";
}
Note(std::string _surname, std::string _name, std::string _phone,
std::string _bh, std::string _socials) {
surname = _surname;
name = _name;
phone = _phone;
birthday = _bh;
socials = _socials;
}
std::string getSurname() {
return surname;
}
std::string getName() {
return name;
}
std::string getPhone() {
return phone;
}
std::string getBirthday() {
return birthday;
}
};
int main()
{
//std::string arr[100];
Note nt{ "Andreeva", "Ulyana", "88005553535", "24.04.2004", {"Youtube"}};
std::cout << nt.getBirthday();
}
This string don't work:
socials = _socials;
Idk how to pass the static string (i can't use the vector or dynamic array). I try to create array in main(), and pass to object of Class, this doesn't help too.
Share Improve this question edited Nov 22, 2024 at 4:00 alfC 16.2k4 gold badges76 silver badges148 bronze badges asked Nov 22, 2024 at 2:31 liloflilof 97 bronze badges 13 | Show 8 more comments2 Answers
Reset to default 1You are trying to assign a single std::string
to an array of std::string
s, which will not work. You would have to pass in an array instead and copy it into your member array. But, since you say you can't use std::vector
, and passing a std::array
would waste a lot of memory in this example, I would suggest passing in a std::initializer_list
instead, eg:
#include <iostream>
#include <string>
#include <initializer_list>
#include <algorithm>
class Note {
private:
std::string surname;
std::string name;
std::string phone;
std::string birthday;
std::string socials[100];
public:
void print() {
std::cout << name << " " << surname << "\n";
}
Note(std::string _surname, std::string _name, std::string _phone,
std::string _bh, std::initializer_list<std::string> _socials) {
surname = _surname;
name = _name;
phone = _phone;
birthday = _bh;
std::copy_n(_socials.begin(), std::min(_socials.size(), std::size(socials)), socials);
}
std::string getSurname() {
return surname;
}
std::string getName() {
return name;
}
std::string getPhone() {
return phone;
}
std::string getBirthday() {
return birthday;
}
};
int main()
{
Note nt{ "Andreeva", "Ulyana", "88005553535", "24.04.2004", {"Youtube"}};
std::cout << nt.getBirthday();
}
This goes beyond your actual question, but it is where you seem to be heading, given your comments.
#include <array>
#include <iostream>
#include <vector>
#include <string>
class Note {
private:
std::string surname;
std::string name;
std::string phone;
std::string birthday;
std::array<std::string, 100> socials;
public:
void print() {
std::cout << name << " " << surname << "\n";
}
Note(std::string _surname, std::string _name, std::string _phone,
std::string _bh, std::array<std::string, 100> const& _socials) {
surname = _surname;
name = _name;
phone = _phone;
birthday = _bh;
socials = _socials;
}
...
本文标签: How to pass static string array to constructor CStack Overflow
版权声明:本文标题:How to pass static string array to constructor C++? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306027a1932803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
std::array
if you want assignment to work, or better still usestd::vector
. – paddy Commented Nov 22, 2024 at 2:35std::vector
then why do you have#include <vector>
in your code? – Remy Lebeau Commented Nov 22, 2024 at 6:49