【问题标题】:no matching function for call to (Classes, c++)没有匹配的函数调用(类,C++)
【发布时间】:2017-05-08 13:30:42
【问题描述】:

我是 C++ 的初级,我正在制作程序,通过在我的课堂上转换为方法来显示一些数据。它还没有真正完成,但是当我创建一个类对象时,它在“Passport Info”行对我说“没有匹配的调用函数”。这是我的代码:

#include <iostream>
#include <string>

using namespace std;

class Passport {
private:
    int _size = 6;
    std::string* class_people;
    std::string* class_birth;
    std::string people[6] = {
        "bro1", "bro2", "bro3", "bro4", "bro5", "bro6"
    };
    std::string birth[6] = { "1995", "1994", "1996", "1994", "2001", "1990" };

public:
    Passport(std::string people, std::string birth)
    {
        class_people = new string[_size];
        class_birth = new string[_size];

        for (int ix = 0; ix < _size; ix++) {
            class_people[ix] = people[ix];
            class_birth[ix] = birth[ix];
        }
    }

    void show_data()
    {
        for (int px = 0; px < _size; px++) {
            cout << class_people[px] << endl;
            cout << class_birth[px] << endl;
        }
    }
};

int main()
{
    setlocale(LC_ALL, "Russian");

    Passport Info;
    Info.show_data();
    system("pause");

    return 0;
};

我还想在 Passport 类的私有部分创建指向我的字符串变量的指针,以便在构造函数中使用,但是如何?谢谢。

【问题讨论】:

  • 如果你想构造一个Passport 的实例而不传递任何参数,你需要为它定义一个默认构造函数。
  • @CaptainObvlious:回答这个问题。
  • 首选std::vector&lt;std::string&gt; 而不是new String[]。通过const &amp; 获取参数。将您的私人默认设置为const
  • @CaptainObvlious:谢谢,它成功了。 :)
  • @MooingDuck:好的,我会的。我只是新人,所以记住一切并不容易。 :)

标签: c++ class pointers dynamic constructor


【解决方案1】:

问题是您的Passport 类没有Passport Info; 试图调用的默认构造函数。

要么给你的Passport类添加一个默认构造函数,要么给Passport Info;添加一些参数例如:

int main()
{
    setlocale(LC_ALL, "Russian");

    Passport Info("Vladimir", "1995");
    Info.show_data();
    system("pause");

    return 0;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2016-11-23
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多