奇怪的行为(纠正) [英] Strange behaviour (corrected)
问题描述
代码:
#include< iostream>
#include< ctime>
#include< vector>
#include< cstddef>
struct SomeClass
{
typedef std :: vector< intTypeVector;
TypeVector vec;
enum {VectorSize = 10};
public:
SomeClass();
};
SomeClass :: SomeClass():vec(VectorSize)
{
使用命名空间std;
for(TypeVector :: size_type i = 0; i< vec.size(); ++ i)
{
vec [i] = rand();
cout<< vec [i]<<"" " ;;
}
cout<<" \ n\ n";
} >
int main()
{
使用命名空间std;
srand(time( 0));
const size_t SIZE = 10;
typedef vector< SomeClassVector;
cout<< \ nCreating vector with<< SIZE<< " SomeClass对象......"<<
endl;
向量vec(SIZE);
}
产生:
john @ john-desktop:〜/ Projects / Other / anjuta1 / src $ ./foobar_cpp
用10个SomeClass对象创建向量...
73703028 1208935909 602693459 1501639085 1773871829 541492682
713359358 1018154590 823363404 280191048
>
john @ john-desktop:〜/ Projects / Other / anjuta1 / src $
(10个值)而不是100个值。它是编译器缺陷,还是我错过了什么?
The code:
#include <iostream>
#include <ctime>
#include <vector>
#include <cstddef>
struct SomeClass
{
typedef std::vector<intTypeVector;
TypeVector vec;
enum { VectorSize= 10 };
public:
SomeClass();
};
SomeClass::SomeClass():vec(VectorSize)
{
using namespace std;
for(TypeVector::size_type i= 0; i< vec.size(); ++i)
{
vec[i]= rand();
cout<<vec[i]<<" ";
}
cout<<"\n\n";
}
int main()
{
using namespace std;
srand(time(0));
const size_t SIZE=10;
typedef vector<SomeClassVector;
cout<< "\nCreating vector with "<< SIZE<< " SomeClass objects..."<<
endl;
Vector vec(SIZE);
}
in my system produces:
john@john-desktop:~/Projects/Other/anjuta1/src$ ./foobar_cpp
Creating vector with 10 SomeClass objects...
73703028 1208935909 602693459 1501639085 1773871829 541492682
713359358 1018154590 823363404 280191048
john@john-desktop:~/Projects/Other/anjuta1/src$
(10 values) instead of 100 values. Is it a compiler defect, or am I
missing something?
推荐答案
./ foobar_cpp
用10个SomeClass对象创建向量...
73703028 1208935909 602693459 1501639085 1773871829 541492682
713359358 1018154590 823363404 280191048
john @ john-desktop:〜/ Projects / Other / anjuta1 / src
./foobar_cpp
Creating vector with 10 SomeClass objects...
73703028 1208935909 602693459 1501639085 1773871829 541492682
713359358 1018154590 823363404 280191048
john@john-desktop:~/Projects/Other/anjuta1/src
(10个值)代替100个值。这是一个编译器缺陷,还是我错过了什么?
(10 values) instead of 100 values. Is it a compiler defect, or am I
missing something?
Ioannis Vranos写道:
Ioannis Vranos wrote:
[...]
矢量vec(SIZE);
[...]
Vector vec(SIZE);
您的类缺少复制构造函数。 />
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
Your class is missing the copy constructor.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于奇怪的行为(纠正)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!