Stroustrup 3.7.1 - “vector” [英] Stroustrup 3.7.1 - "vector"
问题描述
这是没有任何问题的代码:
------------------------- ----------------------------
#include< iostream>
#include< string>
#include< vector>
struct Entry {
std :: string name;
int e_num;
};
输入phone_book [10];
void print_entry(int i){
std :: cout<< phone_book [i] .name
<< "
<< phone_book [i] .e_num
<< " \ n";
}
int main(){}
------------ -----------------------------------------------
现在如果我把整件事包起来上面/主要/进入一个函数或者如果我将
放在整体中上面主要进入/ main /我得到了这个错误:
03_371.cpp:在函数''void c_style_code()'':
03_371.cpp :16:错误:此处不允许使用函数定义
''{''令牌
为什么我能得到它?
this is the code which runs without any trouble:
-----------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
struct Entry {
std::string name;
int e_num;
};
Entry phone_book[10];
void print_entry(int i) {
std::cout << phone_book[i].name
<< " "
<< phone_book[i].e_num
<< "\n";
}
int main() { }
-----------------------------------------------------------
now if i wrap the "whole-thing" above /main/ into a function or if i
put the "whole-thing" above main into the /main/ i got this error:
03_371.cpp: In function ''void c_style_code()'':
03_371.cpp:16: error: a function-definition is not allowed here before
''{'' token
why exactly i got that?
推荐答案
arnuld写道:
arnuld wrote:
这是运行没有任何问题的代码:
-------------------------------------- ---------------
#include< iostream>
#include< string>
#include< vector>
struct Entry {
std :: string name;
int e_num;
};
输入phone_book [10];
void print_entry(int i){
std :: cout<< phone_book [i] .name
<< "
<< phone_book [i] .e_num
<< " \ n";
}
int main(){}
-------- -------------------------------------------------- -
现在如果我把整件事包起来的话上面/主要/进入一个函数或者如果我将
放在整体中上面主要进入/ main /我得到了这个错误:
03_371.cpp:在函数''void c_style_code()'':
03_371.cpp :16:错误:此处不允许使用函数定义
''{''令牌
为什么我得到了这个?
this is the code which runs without any trouble:
-----------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
struct Entry {
std::string name;
int e_num;
};
Entry phone_book[10];
void print_entry(int i) {
std::cout << phone_book[i].name
<< " "
<< phone_book[i].e_num
<< "\n";
}
int main() { }
-----------------------------------------------------------
now if i wrap the "whole-thing" above /main/ into a function or if i
put the "whole-thing" above main into the /main/ i got this error:
03_371.cpp: In function ''void c_style_code()'':
03_371.cpp:16: error: a function-definition is not allowed here before
''{'' token
why exactly i got that?
你是什么意思包装整件事?这样的事情:
int main()
{
#include< iostream>
#include< string>
#include< vector>
struct Entry {
std :: string name;
int e_num;
};
输入phone_book [10];
void print_entry(int i){
std :: cout<< phone_book [i] .name
<< "
<< phone_book [i] .e_num
<< " \ n";
}
}
在这种情况下,问题在于你不能在其他
函数中定义函数,你不应该在函数中使用#include statemens。为什么
你想把它全部放在一个函数中吗?
What do you mean by "wrap the whole-thing"? Something like this:
int main()
{
#include <iostream>
#include <string>
#include <vector>
struct Entry {
std::string name;
int e_num;
};
Entry phone_book[10];
void print_entry(int i) {
std::cout << phone_book[i].name
<< " "
<< phone_book[i].e_num
<< "\n";
}
}
In this case, the problem is that you can''t define functions within other
functions, and you shouldn''t use #include statemens within functions. Why
exactly do you want to put it all inside a function?
arnuld写道:
arnuld wrote:
现在,如果我将整体包裹起来上面/主要/进入一个函数或者如果我将
放在整体中以上主要进入/ main /我收到此错误:
now if i wrap the "whole-thing" above /main/ into a function or if i
put the "whole-thing" above main into the /main/ i got this error:
请发布整码无法编译。
-
Salu2
Please post the "whole-code" that fails to compile.
--
Salu2
你的意思是什么?包裹整件事?这样的事情:
NO,但几乎像你一样:
#include< iostream>
#include< ; string>
#include< vector>
int main(){
struct Entry {
std :: string name;
int e_num;
};
条目phone_book [10 ];
void print_entry(int i){
std :: cout<< phone_book [i] .name
<< "
<< phone_book [i] .e_num
<< " \ n";
}
}
What do you mean by "wrap the whole-thing"? Something like this:
NO but nearly something same like you did:
#include <iostream>
#include <string>
#include <vector>
int main() {
struct Entry {
std::string name;
int e_num;
};
Entry phone_book[10];
void print_entry(int i) {
std::cout << phone_book[i].name
<< " "
<< phone_book[i].e_num
<< "\n";
}
}
在这种情况下,问题是你不能在其他
函数中定义函数,
In this case, the problem is that you can''t define functions within other
functions,
OK。
OK.
并且你不应该在函数中使用#include statemens。
and you shouldn''t use #include statemens within functions.
我永远不会这样做。
i never do.
为什么你想把它全部放在一个函数中?
Why exactly do you want to put it all inside a function?
没有特别的理由,我是一个真实的编码新手&也是一个C ++新手,
所以我正在试验Stroustrup的代码&无法找到
回答一些问题。我还有2个问题,我会在这里放一个
关于/范围检查/使用/ template /:
模板< class Tclass Vec:public vector< ; T {
public:
Vec():vector< T(){}
Vec(int s):vector< T> (s){}
T& operator [](int i){return at(i); }
const T& operator [](int i)const {return at(i); }
i在这段代码中理解这些东西:
1.)我们在这里定义一个新模板。
2.)/ public:/ tag中的第2行是构造函数。
3.)/ public:/ tag中的最后2行是成员函数。
4.)vector< Tis父类的类Vec。
我不明白的是:
1.)我们为什么使用/ class< T> / in" template"当我们在最后2行中使用< Tin
vector< T
2.)时,我们几乎做同样的事情。唯一的区别是
" const"。为什么这个?为什么不只使用一行?
- arnuld
arnuld.blogspot.com
no special reason, i am a real-life coding newbie & also a C++ newbie,
so i was experimenting with Stroustrup''s code & was not able to find
answers to some problems. i have 2 more problems, i will put one here
regarding /range checking/ using a /template/:
template<class Tclass Vec : public vector<T{
public:
Vec() : vector<T() { }
Vec(int s) : vector<T>(s) { }
T& operator[] (int i) { return at(i); }
const T& operator[] (int i) const { return at(i); }
i understand these things in this code:
1.) we are defining a new template here.
2.) 1st 2 line in /public:/ tag are constructors.
3.) last 2 lines in /public:/ tag are "member functions".
4.) vector<Tis parent class of "class Vec".
what i dont understand is:
1.) why we are using /class<T>/ in "template" when we are using "<Tin
vector<T"
2.) in last 2 lines we nearly do the same thing. the only difference is
"const". why this? why not use only one line?
-- arnuld
arnuld.blogspot.com
>
这篇关于Stroustrup 3.7.1 - “vector”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!