在串联COUT声明数组大小 [英] Concatenating an array size in cout statement
问题描述
我试图输出在我的数组元素的对象的数量,但是该语法是不一样的,因为它是用于Java
I'm trying to output the number of element-objects in my array, but the syntax for that is not the same as it is for Java:
// print list of all messages to the console
void viewSent()
{
cout << "You have " << sent.size() << " new messages.\n";//Error: left of '.size' must have class/struct,union
std::cout << "Index Subject" << '\n';
for (size_t i = 0; i < sent.size(); ++i)
{
std::cout << i << " : " << sent[i].getSubject() << '\n';
}
}
如果.size不C ++语法的工作有哪些呢?
if the .size doesn't work in C++ syntax, what does?
推荐答案
Java数组的C ++相当于是的std ::矢量
。 sent.size()
是让大小的正确方法。
The C++ equivalent of a Java array is std::vector
. sent.size()
is the correct way to get the size.
您不发表您的定义发送
,但它应该是的std ::矢量&lt;&YourObject GT;发送;
,也许还指定初始大小和/或值
You didn't post your definition of sent
but it should be std::vector<YourObject> sent;
, perhaps with initial size and/or values also specified.
我猜你试图用C数组 - 不这样做,C风格的数组有奇怪的语法和行为历史原因和实在没有必要在C ++中曾经使用它们
I'm guessing you tried to use a C-style array -- don't do that, C-style arrays have strange syntax and behaviour for historical reasons and there is really no need to use them ever in C++.
这篇关于在串联COUT声明数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!