You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

未调用对应函数却报“No matching function for call”编译错误求助

编译错误:No matching function for call to ‘RedCard::RedCard()’ 解决方法

问题描述

在完成大学C++课程编程任务时,编译heart.cpp触发如下错误——代码中未直接调用RedCard类构造函数,却提示找不到匹配的RedCard::RedCard():

编译错误信息

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

相关代码片段

heart.cpp原代码:

#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
}

heart.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

redcard.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

错误原因

C++中,子类构造函数执行时必须先初始化基类部分。如果子类构造函数没有显式调用基类构造函数,编译器会自动尝试调用基类的默认构造函数(无参构造)。

但RedCard类仅定义了带int参数的构造函数RedCard(int v),未提供无参的默认构造函数,因此编译器找不到匹配的构造函数,触发报错。

解决方案

修改Heart的构造函数,通过初始化列表显式调用基类RedCard的带参构造函数,同时复用基类逻辑简化代码:

修改后的heart.cpp代码

#include <iostream>
#include <cstdlib>
#include "heart.h"

// 使用初始化列表显式调用基类RedCard的构造函数,传递参数v
Heart::Heart(int v) : RedCard(v) {
    SetSuit('H'); // 仅设置花色,基类已完成值和颜色的初始化
}

string Heart::Description() const   
{
    // 复用基类RedCard的Description结果,追加花色信息,避免重复代码
    return RedCard::Description() + ", Suit = " + GetSuit();
}

说明

  1. 显式调用基类构造函数:通过RedCard(v)在初始化列表中调用基类的带参构造函数,满足基类的初始化要求,直接解决编译错误。
  2. 简化代码逻辑:基类RedCard的构造函数已经完成SetValue(v)和SetColor("red")的操作,Heart构造函数只需设置自身特有的花色suit即可。
  3. 复用基类方法:Description方法直接复用基类的结果,再追加花色信息,减少冗余的重复代码,符合继承的设计意图。

内容的提问来源于stack exchange,提问作者SuperDoom1 Unrevealed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:15:55