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
  • 1 You can use std::array if you want assignment to work, or better still use std::vector. – paddy Commented Nov 22, 2024 at 2:35
  • i need more then one item. for example, socials : {"Youtube", "Instagram", ...} – lilof Commented Nov 22, 2024 at 2:36
  • 2 What do you mean "I still need static"? Nothing here is static. – paddy Commented Nov 22, 2024 at 2:38
  • 2 Example: godbolt.org/z/Y6q1Kjs8f – paddy Commented Nov 22, 2024 at 2:47
  • 1 @lilof if you can't use std::vector then why do you have #include <vector> in your code? – Remy Lebeau Commented Nov 22, 2024 at 6:49
 |  Show 8 more comments

2 Answers 2

Reset to default 1

You are trying to assign a single std::string to an array of std::strings, 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