2016-11-29 5 views
2

私はC++を初めて使いました。学校の割り当てには、基底クラスを作成し、2つの派生クラスを作成し、2つの関連するすべての関数の出力を表示する必要があります派生クラス。最初の派生クラスのみが含まれています

問題は、テストファイルに2つの派生クラスを含めるときに、最初に含めるクラスのみが問題になります。私がそれらを含む順序を逆にすれば、現在最初に含まれているものが動作しますが、2番目のものは動作しません。

これはなぜ起こっているのでしょうか?ここで

#ifndef CASHPAYMENT_H 
#define CASHPAYMENT_H 

#include <string> 
#include "Payment.h" 
using namespace std; 

class CashPayment: public Payment { 

    public: 

     // Creates a CashPayment with an amount of 0 
     CashPayment(); 

     // Creates a CashPayment with the given amount 
     CashPayment(double a); 

     // Creates a string out of all the CashPayment details 
     string paymentDetails(); 
}; 
#endif 

CreditCardPayment.hファイルです:

#include "Payment.h" 
#include "CashPayment.h" // This one will work 
#include "CreditCardPayment.h" // This one won't. Unless you switch them 
#include <iostream> 
#include <string> 
using namespace std; 

// Tests the CashPayment and CreditCardPayment classes, derived from the Payment clas 
int main() { 

    CashPayment firstCash(420); 
    cout << "First Cash Payment" << endl 
     << "Amount: " << firstCash.getAmount() << endl 
     << firstCash.paymentDetails() << endl; 

    CreditCardPayment firstCredit("Mingwu Chen", 123456789, 11, 2016, 100); 
    cout << "Number: " << firstCredit.getNumber() << endl 
     << "Month: " << firstCredit.getMonth() << endl 
     << "Year: " << firstCredit.getYear() << endl 
     << "Amount: " << firstCredit.getAmount() << endl 
     << firstCredit.paymentDetails() << endl; 
} 

はここに私のCashPayment.hファイルです:

#ifndef CASHPAYMENT_H 
#define CASHPAYMENT_H 

#include <string> 
#include "Payment.h" 
using namespace std; 

class CreditCardPayment: public Payment { 

    private: 
     string name; 
     int cardNumber; 
     int expMonth; 
     int expYear; 

    public: 

     // Creates a CreditCardPayment with an amount of 0 
     CreditCardPayment(); 

     // Creates a CreditCardPayment with the given card holder name, card number, expiry month, expiry year, and amount 
     CreditCardPayment(string n, int c, int m, int y, double a); 

     // Creates a string out of all the CreditCardPayment details 
     string paymentDetails(); 

     // Returns the card holder name 
     string getName(); 

     // Returns the card number 
     int getNumber(); 

     // Returns the expiry month 
     int getMonth(); 

     // Returns the expiry year 
     int getYear(); 

     // Sets the card holder name t0 the given name 
     void setName(string n); 

     // Sets the card number to the given number 
     void setNumber(int c); 

     // Sets the expiry month to the given month 
     void setMonth(int m); 

     // Sets the expiry year to the given year 
     void setYear(int y); 
}; 
#endif 

ここPayment.hファイルだ

は、ここに私のテストファイルです:

#ifndef PAYMENT_H 
#define PAYMENT_H 

#include <string> 
using namespace std; 

class Payment { 

    private: 
     double amount; 

    public: 
     // Creates a Payment with an amount of 0 
     Payment(); 

     // Creates a Payment with the given amount 
     Payment(double a); 

     // Returns the amount 
     double getAmount(); 

     // Sets the amount to the given value 
     void setAmount(int a); 

     // Creates a string out of all the Payment details 
     string paymentDetails(); 
}; 
#endif 

私はこれを理解しようと徹夜してきました。私は怪物的に困惑しています。すべてのアイデアや提案は非常に高く評価されます。

(PS:ここで多くの人が名前空間stdを使用したいとしていないようですが、それは私の先生が何を望んだ)

+1

_ "それは私の先生が望んでいるものです。 –

+0

ヘッダーファイル内の 'using namespace std;'は、複数の人が同じソースで作業している大規模なプロジェクトで大混乱を招く優れた方法です。しかし、それは学生のプロジェクトを傷つけることはほとんどありません。 –

+0

ヒント:ヘッダーファイルをユニークにするものについて考えてみましょう。 copypasta monsta gotchaのように見えます – mascoj

答えて

4

どちらのヘッダファイルがあります。このよう

#ifndef CASHPAYMENT_H 
#define CASHPAYMENT_H 

を、1がある方最初に含まれるのは、内容が実際にコンパイルされるものです。

+0

私はそれを信じられませんでした。どうもありがとうございました! –

1
#ifndef CASHPAYMENT_H 
#define CASHPAYMENT_H 

(stuff) 

#endif 

これはinclude-guardと呼ばれ、プログラムのコンパイル時に複数のファイルが含まれることを停止します。

3つのヘッダーファイルすべてにCASHPAYMENT_Hを含めるべきではありません。各ファイルにはユニーク include-guardが必要です。それはあなたのプロジェクトで一意である限り、実際には何の問題もなく、大文字のヘッダーファイル名はこのための良い慣習です。

#ifndefする代わりに、あなたが同じことを行い#pragma onceを、使用することができたよう:

#pragma once 

(stuff) 

(。ファイルの末尾に#endifはありません注意してください)

それはC言語ではありません++これはコンパイラのほとんどすべてでサポートされており、少しはっきりしています。

+3

通常の '#pragma once'警告です。 'once'、そしてすべての' #pragma'はC++への実装特有の拡張です。これらは標準の一部ではなく、すべてのC++コンパイラでサポートされているわけではありません。実際、 '#pragma'がサポートされていない場合、コンパイラはそれを黙って捨てることができ、コメディのハイ・ジンクスにつながります。使用する前にコンパイラがプラグマをサポートしていることを確認してください。 「一度」に特有のことですが、標準から外したいくつかの問題があります。これは、マップされたドライブとシンボリックリンクを利用する複雑なビルドシステムによって容易に混乱します。慎重に使用してください。 – user4581301

+0

詳細な返信をありがとう!私はそれを見ていないとは信じられません。それは、私がCashPayment.hファイルをコピーし、それを編集してCreditCardPayment.hファイルを作成したからです。しかし、私はそれを変更したときにこれを見逃しているに違いない。ありがとうございました! –

-1

コードをコピーする必要があります。IDEのツールからクラスを作成する方がよいでしょう。それは自動的にヘッダーディフェンダーを追加します。お支払い詳細機能は、仮想である必要があります。

+0

どのIDEをお勧めしますか?私の先生は私たちにSCITEを使ってもらいました。私はそれを嫌いです。私はもっと良いものを探しています。 –

+0

QtCreatorまたはVS. Linuxを使用している場合は、QtCreatorが最適です。 –

0

クレジットカード決済の場合。hファイルに入れてください:

#ifndef CREDITCARDPAYMENT_H 
#define CREDITCARDPAYMENT_H 
関連する問題