我不知道发生了什么。我需要在明天午夜前完成这项工作,但无论出于什么原因,它都行不通。
这是我试图编译的代码:
#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
}