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.
13
You 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;
}
...
2