2016-04-29 21 views
0

私は現在、私のメインで配列を宣言し、ユーザー入力と配列に格納します。私は始めましたが、私はエラーbinary '>>': no operator found which takes a right-hand operand of type 'CDistance'に走っています私は#include <string>も私のコードに含まれています。このエラーは、機能void inputDist(CDistance distList[], int size)の下で発生します。以下は完全なコードです。すべてのフィードバックは高く評価されます。ありがとう。C++オペレータ ">>"はこれらのオペランドと一致しません。

#include <iostream> 
#include <conio.h> 
#include <string> 

using namespace std; 

class CDistance 
{ 
private: 
    int feet; 
    int inches; 
    int feet2; 
    int inches2; 

public: 
    CDistance(); 
    CDistance(int, int, int, int); 
    ~CDistance(); 
    CDistance printAndAdd(const CDistance distList[], int size); 
    void setDistt(); 
    void printDistt() const; 
    void add(const CDistance&) const; 
    void subtract(const CDistance&) const; 
    void menu(const CDistance&) const; 
    void inputDist(CDistance distList[], int size); 
}; 

CDistance::CDistance() 
{ 
    feet; 
    inches; 
    feet2; 
    inches2; 
} 

CDistance::CDistance(int f, int i, int f2, int i2) 
{ 
    feet = f; 
    inches = i; 
    feet2 = f2; 
    inches2 = i2; 
} 

CDistance::~CDistance() 
{ 
} 

void CDistance::setDistt() 
{ 
    cout << "Enter the first set of feet: "; 
    cin >> feet; 
    cout << "\nEnter the second set of feet: "; 
    cin >> feet2; 
    cout << "\nEnter the first set of inches: "; 
    cin >> inches; 
    cout << "\nEnter the second set of inches: "; 
    cin >> inches2; 
} 

void CDistance::printDistt() const 
{ 
    cout << "Feet: " << feet << "," << feet2 << endl << "Inches: " << inches << "," << inches2 << endl; 
} 

void CDistance::add(const CDistance& total) const 
{ 
    int totFeet = feet + feet2; 
    int totInches = inches + inches2; 
    if (totInches >= 12) 
    { 
     totInches = totInches/12; 
     int newFeet = totInches; 
     totFeet = totFeet + newFeet; 
    } 
    cout << totFeet << " feet" << endl; 
    cout << totInches << " inches" << endl; 
} 

void CDistance::subtract(const CDistance& total) const 
{ 
    int totFeet = feet - feet2; 
    int totInches = inches - inches2; 
    if (totInches >= 12) 
    { 
     totInches = totInches/12; 
     int newFeet = totInches; 
     totFeet = totFeet - newFeet; 
    } 
    cout << totFeet << " feet" << endl; 
    cout << totInches << " inches" << endl; 
} 

void CDistance::menu(const CDistance& total) const 
{ 
    CDistance m(feet, inches, feet2, inches2); 
    int choice; 
    bool menuGo = true; 


    while (menuGo != false) 
    { 
     { 
      cout << 
       "\nWhat would you like to do?" 
       "\n1: Add " 
       "\n2: Subtract " 
       "\n3: Exit" << endl; 
      cin >> choice; 
     } 

     switch (choice) 
     { 
     case 1: 
      cout << "You chose to add" << endl; 
      m.add(total); 
      break; 
     case 2: 
      cout << "You chose to subtract" << endl; 
      m.subtract(total); 
      break; 
     case 3: 
      cout << "Please enter 5 digits to enter into the array: "; 
      m.inputDist; 
     case 4: 
      cout << "Goodbye" << endl; 
      menuGo = false; 
      break; 
     default: 
      cout << "Not a valid choice." << endl; 
      cout << "Choose again." << endl; 
      cin >> choice; 
      break; 
     } 
    } 
} 

void inputDist(CDistance distList[], int size) 
{ 
    int dist = 0; 
    for (int i = 0; i < 6; i++) 
    { 
     cin >> distList[i]; 
    } 
} 

//CDistance printAndAdd(const CDistance distList[], int size); 
int main() 
{ 
    CDistance d1, d2(0, 0, 0, 0); 

    CDistance distList[5]; 

    d1.setDistt(); 
    d1.printDistt(); 
    d1.menu(d2); 
    inputDist(distList, 0); 
    _getch(); 
    return 0; 
} 
+5

これはまさにそのことです...演算子は定義されていないので、 'cin'から' CDistance'を読むことができます。 – immibis

+1

** conio.h **の使用を中止してください。 _C++標準ライブラリの一部ではありません。 – Destructor

+0

これは、あなたが投稿しているもの以外のコンパイルエラーを全面的に持っています。それらを最初に修正し、_then_、まだ助けが必要な場合は、もう一度聞くことができます。 –

答えて

0

CDistanceはカスタムタイプであり、cinには値を設定するインターフェイスがありません。

void inputDist(CDistance distList[], int size) 
{ 
    int dist = 0; 
    for (int i = 0; i < 6; i++) 
    { 
     cin >> distList[i]; //<- invalid 
    } 
} 

代わりに以下のことを試してみてください。

void inputDist(CDistance distList[], int size) 
{ 
    int dist = 0; 
    for (int i = 0; i < 6; i++) 
    { 
     int feet; cin >> feet; 
     int inches; cin >> inches; 
     int feet2; cin >> feet2; 
     int inches2; cin >> inches2; 
     distList[i] = CDistance(feet,inches,feet2,inches2); 
    } 
} 
2

あなたがoperator>>あなた自身を宣言するようになったカスタムタイプでcin >> variableを使用したい場合はそうでない場合、コンパイラは、あなたが実際に読んでもらいたい方法を知ることができません値。この場合にはそれを行うための可能な方法は次のようになります。

friend std::istream& operator>>(std::istream& is, CDistance& dist); 

は、クラスの体内フレンド関数として宣言し、その後にまた

std::istream& operator>>(std::istream& is, CDistance& dist) { 
    is >> dist.feet >> dist.inches >> 
      dist.feet2 >> dist.inches2; 
    return is; 
} 

のように外にそれを定義しますあなたのデフォルトのコンストラクタは絶対に何もしません:

CDistance::CDistance() 
{ 
    feet;  // evaluate feet and discard it as you don't do anything with it. 
    inches;  // same 
    feet2;  // same 
    inches2; // same 
} 

あなたはデフォルト値ead:

CDistance::CDistance() 
{ 
    feet = inches = feet2 = inches2 = 0; 
} 

はすべてデフォルトで0に設定されます。

+0

@RSahuは頭をアップしてくれてありがとう、そのミスを完全に逃した... –

関連する問題