将向量传递给函数 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7677007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing vector to function c++
提问by TT12
I have a main.cpp test.h and test.cpp> I am trying to pass my vector through so i can use it in test.cpp but i keep getting errors.
我有一个 main.cpp test.h 和 test.cpp> 我试图通过我的向量,所以我可以在 test.cpp 中使用它,但我不断收到错误。
//file: main.cpp
int main(){
vector <Item *> s;
//loading my file and assign s[i]->name and s[i]-address
tester(s);
}
//file: test.h
#ifndef TEST_H
#define TEST_H
struct Item{
string name;
string address;
};
#endif
//file: test.cpp
int tester(Item *s[]){
for (i=0; i<s.sizeof();i++){
cout<< s[i]->name<<" "<< s[i]->address<<endl;
}
return 0;
}
---------------errors--------
In file included from main.cpp:13:
test.h:5: error: astringa does not name a type
test.h:6: error: astringa does not name a type
main.cpp: In function aint main()a:
main.cpp:28: error: cannot convert astd::vector<Item*, std::allocator<Item*> >a to aItem**a for argument a1a to aint tester(Item**)a
回答by Chad
A std::vector<T>
and T* []
are not compatible types.
Astd::vector<T>
和T* []
是不兼容的类型。
Change your tester()
function signature as follows:
更改您的tester()
函数签名如下:
//file: test.cpp
int tester(const std::vector<Item>& s) // take a const-reference to the std::vector
// since you don't need to change the values
// in this function
{
for (size_t i = 0; i < s.size(); ++i){
cout<< s[i]->name<<" "<< s[i]->address<<endl;
}
return 0;
}
There are several ways you could pass this std::vector<T>
and all have slightly different meanings:
有几种方法可以传递它,std::vector<T>
并且它们的含义都略有不同:
// This would create a COPY of the vector
// that would be local to this function's scope
void tester(std::vector<Item*>);
// This would use a reference to the vector
// this reference could be modified in the
// tester function
// This does NOT involve a second copy of the vector
void tester(std::vector<Item*>&);
// This would use a const-reference to the vector
// this reference could NOT be modified in the
// tester function
// This does NOT involve a second copy of the vector
void tester(const std::vector<Item*>&);
// This would use a pointer to the vector
// This does NOT involve a second copy of the vector
// caveat: use of raw pointers can be dangerous and
// should be avoided for non-trivial cases if possible
void tester(std::vector<Item*>*);
回答by NPE
- You should
#include <string>
. string name
should readstd::string name
etc. Same goes forstd::vector
.- You're calling
tester()
with avector
, yet it expects an array (the two are not interchangeable). s.sizeof()
is incorrect for both an array and a vector; for the latter, uses.size()
or, better yet, use an iterator.
- 你应该
#include <string>
。 string name
应该阅读std::string name
等。同样适用于std::vector
。- 您正在
tester()
使用 a 进行调用vector
,但它需要一个数组(两者不可互换)。 s.sizeof()
对数组和向量都不正确;对于后者,使用s.size()
或者更好的是使用迭代器。
These are just the errors that immediately jump out; there may be more.
这些只是立即跳出的错误;可能还有更多。
回答by Griwes
Pass it as std::vector<Item *> &
(reference to vector) and use iterator to iterate through it.
将其作为std::vector<Item *> &
(对向量的引用)传递并使用迭代器对其进行迭代。
回答by crashmstr
A vector
is not an array.
Avector
不是数组。
int tester(vector<Item *> &s)
(pass as a reference to avoid copying or if you need to modify)
(作为参考传递以避免复制或需要修改)
You also need to modify your code inside the tester
function to work correctly as a vector.
您还需要修改tester
函数内的代码才能作为向量正常工作。
回答by Philipp
You should fix
你应该修
test.h:5: error: astringa does not name a type
first, probably by using namespace std;
and #include <string>
首先,可能由using namespace std;
和#include <string>
回答by bbtrb
You are missing includes
你缺少的包括
#include <string>
#include <vector>
and you need to use std::string
and std::vector<>
. A std::vector
is not an array, so you should pass the vector as reference
并且您需要使用std::string
和std::vector<>
。Astd::vector
不是数组,因此您应该将向量作为参考传递
int tester(std::vector<Item*> & vec) { //... }
or even as const std::vector<Item*> &
if you are not going to modify the passed vector.
甚至就const std::vector<Item*> &
好像您不打算修改传递的向量一样。
Also, are you sure, that you'll need a vector of pointers? What are you trying to achieve?
另外,您确定需要一个指针向量吗?你想达到什么目的?