将向量传递给函数 c++
我有一个 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**)â
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.
//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**)â
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
#include
。string name
应读取std::string name
等。std::vector
也是如此。vector
调用tester()
,但它需要一个数组(两者不可互换)。s.sizeof()
对于数组和向量都是不正确的;对于后者,使用 s.size() ,或者更好的是,使用迭代器。这些只是立即跳出的错误;可能还有更多。
#include <string>
.string name
should readstd::string name
etc. Same goes forstd::vector
.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.These are just the errors that immediately jump out; there may be more.
向量
不是数组。(作为参考传递,以避免复制或需要修改)
您还需要修改
tester
函数内的代码,以便作为向量正常工作。A
vector
is not an array.(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.您应该首先修复
,可能是通过
using namespace std;
和#include
You should fix
first, probably by
using namespace std;
and#include <string>
您缺少包含内容
,需要使用
std::string
和std::vector
。std::vector
不是数组,因此您应该将向量作为引用传递,甚至作为
const std::vector- 传递。 &
如果您不打算修改传递的向量。另外,您确定需要一个指针向量吗?你想达到什么目的?
You are missing includes
and you need to use
std::string
andstd::vector<>
. Astd::vector
is not an array, so you should pass the vector as referenceor even as
const std::vector<Item*> &
if you are not going to modify the passed vector.Also, are you sure, that you'll need a vector of pointers? What are you trying to achieve?
std::vector
和T* []
不是兼容的类型。按如下方式更改您的
tester()
函数签名:您可以通过多种方式传递此
std::vector
,并且所有方式的含义都略有不同:A
std::vector<T>
andT* []
are not compatible types.Change your
tester()
function signature as follows:There are several ways you could pass this
std::vector<T>
and all have slightly different meanings:将其传递为
std::vector- ; &
(对向量的引用)并使用迭代器对其进行迭代。Pass it as
std::vector<Item *> &
(reference to vector) and use iterator to iterate through it.