[英]C++ printing contents of array that was declared in main
working on program where an array has to be declared in the main (in this case, distList[5]
) and it has to be sent to one member where it has to be filled by user input ( inputDist
) and then sent to another member from there ( printAndAdd
) to be printed and added, as suggested by the name. 在必须在主数组中声明数组的程序上工作(在这种情况下,是
distList[5]
),并且必须将其发送到一个成员,在其中必须由用户输入( inputDist
)填充,然后再将其发送给另一个成员从那里开始( printAndAdd
),按照名称建议进行打印和添加。 I believe I've gotten the array filled, but I'm not sure how to go from there. 我相信我已经把阵列装满了,但是我不确定该怎么做。 any help is appreciated.
任何帮助表示赞赏。
void CDistance::inputDist(CDistance distList[], int size)
{
for (int i = 0; i < 1; i++)
{
cout << "Please enter 5 digits following each one with a space to place into the array: " << endl;
int ui1; cin >> ui1;
int ui2; cin >> ui2;
int ui3; cin >> ui3;
int ui4; cin >> ui4;
int ui5; cin >> ui5;
distList[i] = CDistance(ui1, ui2, ui3, ui4, ui5);
cout << distList;
}
}
void CDistance::printAndAdd(const CDistance distList[], int size)
{
//cout << distList;
}
int main()
{
CDistance distList[5];
CDistance d1, d2(0, 0, 0, 0, 0);
d1.setDistt();
d1.printDistt();
d1.menu(d2);
d1.inputDist(distList, 5);
d1.printAndAdd(distList, 5);
_getch();
return 0;
}
Also, here is the class definition of CDistance: 另外,这是CDistance的类定义:
class CDistance
{
private:
int feet;
int inches;
int feet2;
int inches2;
int five;
public:
CDistance();
CDistance(int, int, int, int, int);
~CDistance();
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);
void printAndAdd(const CDistance distList[], int size);
};
CDistance::CDistance()
{
feet = 0;
inches = 0;
feet2 = 0;
inches2 = 0;
five = 0;
}
CDistance::CDistance(int f, int i, int f2, int i2, int fi)
{
feet = f;
inches = i;
feet2 = f2;
inches2 = i2;
five = fi;
}
CDistance::~CDistance()
{
}
In a comment, you said: 在评论中,您说:
How would I go about that?
我将如何处理? When I try a for loop, trying to print the contents, it gives me the error of "No operator '<<' matches these operands."
当我尝试for循环时,尝试打印内容,这给我的错误是“没有运算符'<<'与这些操作数匹配。” I did try and create an operator overload of << but wasn't sure how to execute it.
我确实尝试过创建<<的运算符重载,但不确定如何执行它。
Declare operator>>
and operator<<
overloads to allow you read a CDistance
from a stream and write a CDistance
to a stream. 声明
operator>>
和operator<<
重载让你读CDistance
从流和写CDistance
到流。
class CDistance;
std::istream& operator>>(std::istream& is, CDistance& distance);
std::ostream& operator<<(std::ostream& os, CDistance const& distance);
Make them friend
s of the class. 让他们成为班上的
friend
。
class CDistance
{
...
friend std::istream& operator>>(std::istream& is, CDistance& distance);
friend std::ostream& operator<<(std::ostream& os, CDistance const& distance);
};
Implement them. 实施它们。
std::istream& operator>>(std::istream& is, CDistance& d)
{
return (is >> d.feet >> d.inches >> d.feet2 >> d.inches2 >> d.five);
}
std::ostream& operator<<(std::ostream& os, CDistance const& distance);
{
// Add a space between the members when writing it out.
return (is << d.feet << " " << d.inches << " "
<< d.feet2 << " " << d.inches2 << " " << d.five);
}
Now you can use 现在您可以使用
Distance d;
std::cin >> d;
std::cout << d << std::endl;
I suggest the following additional improvements: 我建议进行以下其他改进:
You don't need inputDist
and printAndAdd
to be member functions of the class since they don't need access to private
members of the class. 您不需要
inputDist
和printAndAdd
成为该类的成员函数,因为它们不需要访问该类的private
成员。 They can be non-member functions. 它们可以是非成员函数。
void inputDist(CDistance distList[], int size)
{
// for (int i = 0; i < 1; i++)
for (int i = 0; i < size; i++) // Use size, not 1
{
cout << "Please enter 5 digits following each one with a space to place into the array: " << endl;
cin >> distList[i];
cout << distList << endl;
}
}
void printAndAdd(const CDistance distList[], int size)
{
for (int i = 0; i < size; i++)
{
cout << distList[i] << std::endl;
// Not sure what "Add" means to you.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.