如何将静态字符串数组传递给构造函数C++?

问题描述 投票:0回答:1

我的代码

#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();
}

该字符串不起作用:

socials = _socials;

不知道如何传递静态字符串(我不能使用向量或动态数组)。我尝试在 main() 中创建数组,然后传递给 Class 的对象,这也没有帮助。

c++ arrays string class
1个回答
0
投票

这超出了您的实际问题,但考虑到您的评论,这似乎是您的方向。

#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;
    }
...
© www.soinside.com 2019 - 2024. All rights reserved.