[英]passing vector to function c++
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: âstringâ does not name a type
test.h:6: error: âstringâ does not name a type
main.cpp: In function âint main()â:
main.cpp:28: error: cannot convert âstd::vector<Item*, std::allocator<Item*> >â to âItem**â for argument â1â to âint tester(Item**)â
A std::vector<T>
and T* []
are not compatible types. std::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*>*);
将其作为std::vector<Item *> &
(引用向量)传递,并使用迭代器迭代它。
#include <string>
. #include <string>
。 string name
should read std::string name
etc. Same goes for std::vector
. string name
应该读取std::string name
等。对于std::vector
。 tester()
with a vector
, yet it expects an array (the two are not interchangeable). vector
调用tester()
,但是它需要一个数组(这两个数组不可互换)。 s.sizeof()
is incorrect for both an array and a vector; s.sizeof()
对于数组和向量都是不正确的; for the latter, use s.size()
or, better yet, use an iterator. s.size()
或者更好的是使用迭代器。 These are just the errors that immediately jump out; 这些只是立即跳出的错误; there may be more.
可能会有更多。
A vector
is not an array. vector
不是数组。
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
功能中的代码才能正常工作。
You should fix 你应该修复
test.h:5: error: âstringâ does not name a type
first, probably by using namespace std;
首先,可能是通过
using namespace std;
and #include <string>
和
#include <string>
You are missing includes 你缺少包括
#include <string>
#include <vector>
and you need to use std::string
and std::vector<>
. 你需要使用
std::string
和std::vector<>
。 A std::vector
is not an array, so you should pass the vector as reference std::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?
你想要实现什么目标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.