我目前正在研究一些代码,我必须在我的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;
}
正是它所说的......没有操作符定义,可以让你从'cin'中读取'CDistance'。 – immibis
停止使用** conio.h **。它不是_C++标准库_的一部分。 – Destructor
这有编译错误,其他人比你发布的,到处都是。先解决这些问题,然后_then_,如果你仍然需要帮助,你可以再问一次。 –