【问题标题】:Having trouble with constructors which accepts different parameters接受不同参数的构造函数有问题
【发布时间】:2018-07-05 16:50:33
【问题描述】:

我是 C++ 新手,但在处理类和头文件时遇到了问题。我正在尝试创建一个构造函数来接受各种口袋妖怪统计信息,例如字符串和整数。我以前用 java 编写代码,构造函数的赋值相当简单。

口袋妖怪.h

    #ifndef POKEMONS_H
    #define POKEMONS_H
    #include <string>
    #include <iostream>

    using namespace std;

    class Pokemons {
    public:
    Pokemons();

    };
    #endif /* POKEMONS_H */

口袋妖怪.cpp

    #include "Pokemons.h"
    #include <string>

    using namespace std;


        string pokemonName;
        string pokemonType1;
        string pokemonType2;
        int pokemonHP;
        int pokemonAttack;
        int pokemonDefence;
        int pokemonSPAttack;
        int pokemonSPDefence;
        int pokemonSpeed;

        Pokemons::Pokemons(string nm, string tp1, string tp2, int hp, int atk, 
        int def, int satk, int sdef, int spd) {

            pokemonName      = nm;
            pokemonType1     = tp1;
            pokemonType2     = tp2;
            pokemonHP        = hp;
            pokemonAttack    = atk;
            pokemonDefence   = def;
            pokemonSPAttack  = satk;
            pokemonSPDefence = sdef;
            pokemonSpeed     = spd;
        }

main.cpp

    #include <iostream>
    #include "Pokemons.h"

    int main(){

        Pokemons p001;
        p001.Pokemons("Bulbasaur", "Grass", "None", 31,23,45,43,45,12);
        return 0;

    }

我收到以下错误:

Pokemons.cpp:32:9: error: prototype for 'Pokemons::Pokemons(std::string, std::string, std::string, int, int, int, int, int, int)' does not match any in class 'Pokemons'
         Pokemons::Pokemons(string nm, string tp1, string tp2, int hp, int atk, int def, int satk, int sdef, int spd) {


In file included from Pokemons.cpp:14:0:
Pokemons.h:21:7: error: candidates are: constexpr Pokemons::Pokemons(Pokemons&&)
 class Pokemons {


Pokemons.h:21:7: error:                 constexpr Pokemons::Pokemons(const Pokemons&)
Pokemons.cpp:30:9: error:                 Pokemons::Pokemons()
         Pokemons::Pokemons(){}

【问题讨论】:

  • 有几件事你可以用谷歌搜索:“类初始化”、“初始化列表”。除非您必须,否则不要使用构造函数主体。
  • pokemonName ... pokemonSpeed 应该在您的类定义中,而不是在源文件中的全局变量中。
  • 所有成员声明都应该在类定义之内,而不是在它之外。 (它看起来很像对应的 Java。)

标签: c++ class oop object constructor


【解决方案1】:

这里有三个问题。首先,你的构造函数declaredPokemons();,接受零个参数,但是你有一个构造函数defined接受很多参数,所以它们的签名不匹配,并且最终,由于 C++ 中的函数重载,它们引用了不同的函数。尝试在头文件中声明构造函数如下:

class Pokemons {
    public:
    Pokemons(string nm, string tp1, string tp2, int hp, int atk, 
        int def, int satk, int sdef, int spd);
};

现在定义和声明都应该引用同一个函数。

第二个问题在这里:

Pokemons p001;

这个隐式调用没有参数的构造函数。重要的是要了解许多函数在 C++ 中被调用,即使它们没有被显式命名。要解决此问题,您应该按如下方式初始化 p001

Pokemons p001("Bulbasaur", "Grass", "None", 31,23,45,43,45,12);

您还应该删除以下行中的p001.Pokemons("Bulbasaur", "Grass", "None", 31,23,45,43,45,12);。现在,编译器可以将此调用与带有许多参数的构造函数相匹配。

目前的第三个问题是 pokemonName 一直到 pokemonSpeed 是在全局范围内定义在Pokemons 类之外的。这与在 Java 中创建成员 static 具有类似的效果。这些应该放在你的类定义中,使它们成为实例成员:

class Pokemons {
    public:
    Pokemons(string nm, string tp1, string tp2, int hp, int atk, 
        int def, int satk, int sdef, int spd);

    private:

    string pokemonName;
    string pokemonType1;
    string pokemonType2;
    int pokemonHP;
    int pokemonAttack;
    int pokemonDefence;
    int pokemonSPAttack;
    int pokemonSPDefence;
    int pokemonSpeed;
};

【讨论】:

  • 感谢您的回复,但现在我收到以下错误:main.cpp:20:18: error: no matching function for call to 'Pokemons::Pokemons()' Pokemons p001; ^~~~ I Pokemons.h:21:7: 注意:候选人需要 1 个参数,0 提供 Pokemons.h:21:7: 注意:候选人:constexpr Pokemons::Pokemons(Pokemons&&) Pokemons.h:21:7:注意:候选人需要 1 个参数,0 提供 main.cpp:21:14:错误:无效使用 'Pokemons::Pokemons' p001.Pokemons("Bulbasaur", "Grass", "None", 31,23,45, 43,45,12);
  • 我现在感觉有点傻。谢谢你,好心的先生帮助我!。
  • 没问题。与 Java 相比,C++ 的学习曲线相当陡峭,历史也很混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 2016-09-03
  • 1970-01-01
  • 2010-11-10
  • 2014-02-09
  • 2013-01-27
  • 1970-01-01
相关资源
最近更新 更多