代码之家  ›  专栏  ›  技术社区  ›  SuperDoom1 Unrevealed

当我不调用命名函数[重复]时,“没有匹配的函数可供调用”

c++
  •  0
  • SuperDoom1 Unrevealed  · 技术社区  · 1 年前

    我不知道发生了什么。我需要在明天午夜前完成这项工作,但无论出于什么原因,它都行不通。

    这是我试图编译的代码:

    #include <iostream>
    #include <cstdlib>
    #include "heart.h"
    
    Heart::Heart(int v){
    SetValue(v);
    SetColor("red");
    SetSuit('H');
    }
    string Heart::Description() const   
    // Good ol' fashioned copy-n'-paste...
    // You know you love it.
    {
      string d = "Value = ";    // temporary variable used to accumulate result
    int Value = GetValue(); // A bit more complicated than I expected, but only a line's worth of difference after debugging.
      switch (Value)            // Append card value to variable's value
      {
        case 2:   d = d + "2";    break;      // Number cards
        case 3:   d = d + "3";    break;
        case 4:   d = d + "4";    break;
        case 5:   d = d + "5";    break;
        case 6:   d = d + "6";    break;
        case 7:   d = d + "7";    break;
        case 8:   d = d + "8";    break;
        case 9:   d = d + "9";    break;
        case 10:  d = d + "10";   break;
        
        case 11:  d = d + "J";    break;      // Face cards
        case 12:  d = d + "Q";    break;
        case 13:  d = d + "K";    break;
        case 14:  d = d + "A";    break;
    
        default:  d = d + "?";    break;      // Unknown card
      }
    d = d + ", Color = " + GetColor(); // Hm, hm...
    d = d + ", Suit = " + GetSuit(); // Further than that, the suits were ALSO copy-pasted from the color-cards, in their entirety!!
      return d;                 // Return string describing card value
    }
    

    请忽略这些评论,我这样做是为了大学课堂。 在编译过程中,我遇到了以下错误:

    g++ -c heart.cpp
    heart.cpp: In constructor ‘Heart::Heart(int)’:
    heart.cpp:5:19: error: no matching function for call to ‘RedCard::RedCard()’
     Heart::Heart(int v){
                       ^
    In file included from heart.h:10,
                     from heart.cpp:3:
    redcard.h:19:3: note: candidate: ‘RedCard::RedCard(int)’
       RedCard(int v);   // Creates a red card with value v and unknown suit
       ^~~~~~~
    redcard.h:19:3: note:   candidate expects 1 argument, 0 provided
    redcard.h:12:7: note: candidate: ‘RedCard::RedCard(const RedCard&)’
     class RedCard : public Card
           ^~~~~~~
    redcard.h:12:7: note:   candidate expects 1 argument, 0 provided
    redcard.h:12:7: note: candidate: ‘RedCard::RedCard(RedCard&&)’
    redcard.h:12:7: note:   candidate expects 1 argument, 0 provided
    make: *** [makefile:24: heart.o] Error 1
    

    此文件中没有对RedCard的调用,所有这些都在单独的文件中。 以防万一,这是心脏。h

    //
    // heart.h -- CPE 212 -- Project02 -- Classes + Inheritance
    //
    // DO NOT MODIFY OR SUBMIT THIS FILE!!!
    //
    
    #ifndef HEART_H
    #define HEART_H
    
    #include "redcard.h"
    
    class Heart : public RedCard
    {
     private:
      // No additional private members
        
     public:
      // Constructors
      Heart(int v);    // Creates a red heart card with value v
        
      string Description() const;   // Outputs card characteristics - value, color, suit
                                    // Hint: use base class Description method to generate part of 
                                    // the description and append the suit information at the end
    };
    
    #endif
    
    
    
    

    红牌。h

    //
    // redcards.h -- CPE 212 -- Project02 -- Classes + Inheritance
    //
    // DO NOT MODIFY OR SUBMIT THIS FILE!!!
    //
    
    #ifndef REDCARD_H
    #define REDCARD_H
    
    #include "card.h"
    
    class RedCard : public Card
    {
     private:
      // No additional private members
        
     public:
      // Constructors
      RedCard(int v);   // Creates a red card with value v and unknown suit
        
      string Description() const;   // Outputs card characteristics - value and color as a string
                                    // Hint: use base class Description method to generate part of 
                                    // the description and append the color information at the end
    };
    
    #endif
    
    
    

    redcard.cpp(这个编译得很好)

    #include <iostream>
    #include <cstdlib>
    #include "redcard.h"
    
    RedCard::RedCard(int v){
    SetValue(v);
    SetColor("red");
    SetSuit('U');
    }
    string RedCard::Description() const   
    // Good ol' fashioned copy-n'-paste...
    // You know you love it.
    {
      string d = "Value = ";    // temporary variable used to accumulate result
    int Value = GetValue(); // A bit more complicated than I expected, but only a line's worth of difference after debugging.
      switch (Value)            // Append card value to variable's value
      {
        case 2:   d = d + "2";    break;      // Number cards
        case 3:   d = d + "3";    break;
        case 4:   d = d + "4";    break;
        case 5:   d = d + "5";    break;
        case 6:   d = d + "6";    break;
        case 7:   d = d + "7";    break;
        case 8:   d = d + "8";    break;
        case 9:   d = d + "9";    break;
        case 10:  d = d + "10";   break;
        
        case 11:  d = d + "J";    break;      // Face cards
        case 12:  d = d + "Q";    break;
        case 13:  d = d + "K";    break;
        case 14:  d = d + "A";    break;
    
        default:  d = d + "?";    break;      // Unknown card
      }
    d = d + ", Color = " + GetColor(); // And that's the only difference.
      return d;                 // Return string describing card value
    }
    
    1 回复  |  直到 1 年前
        1
  •  2
  •   NathanOliver    1 年前

    Heart 继承自 RedCard ,这意味着当你构建一个 您还需要构建 红牌 它的基础部分。在你的构造函数中,你有

    Heart::Heart(int v) {
        SetValue(v);
        SetColor("red");
        SetSuit('H');
    }
    

    它不会这样做,所以编译器会通过将构造函数更改为

    Heart::Heart(int v) : RedCard() {
        SetValue(v);
        SetColor("red");
        SetSuit('H');
    }
    

    您没有默认的构造函数 红牌 所以这就是为什么你会看到你所做的所有错误。

    你需要做的就是使用 红牌 像这样约束自己

    Heart::Heart(int v) : RedCard(v) {
        SetValue(v);
        SetColor("red");
        SetSuit('H');
    }
    

    现在 RedCard(int) 将使用构造函数,代码应该编译。