2016-04-29 70 views
0

我目前正在研究一些代码,我必须在我的main中声明一个数组,并创建一个函数,允许我接受用户输入并将其存储到数组中。我已经开始了,但我遇到了错误binary '>>': no operator found which takes a right-hand operand of type 'CDistance'我的代码中也包含#include <string>。该错误发生在功能void inputDist(CDistance distList[], int size)以下。以下是完整的代码。所有和任何反馈意见。谢谢。C++没有运算符“>>”匹配这些操作数(<string>包含在头文件中)

#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

如果你想使用cin >> variable与你必须声明自定义类型自己operator>>否则编译器不知道你是怎么真的想读值。一种可能的方法做,在这种情况下将是:

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感谢您的领导,完全错过了那个错误... –

相关问题