将向量传递给函数 c++

发布于 2024-12-08 15:59:41 字数 989 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

少女的英雄梦 2024-12-15 15:59:42
  1. 您应该#include
  2. string name 应读取 std::string name 等。std::vector 也是如此。
  3. 您使用 vector 调用 tester(),但它需要一个数组(两者不可互换)。
  4. s.sizeof() 对于数组和向量都是不正确的;对于后者,使用 s.size() ,或者更好的是,使用迭代器。

这些只是立即跳出的错误;可能还有更多。

  1. You should #include <string>.
  2. string name should read std::string name etc. Same goes for std::vector.
  3. You're calling tester() with a vector, yet it expects an array (the two are not interchangeable).
  4. s.sizeof() is incorrect for both an array and a vector; for the latter, use s.size() or, better yet, use an iterator.

These are just the errors that immediately jump out; there may be more.

许你一世情深 2024-12-15 15:59:42

向量不是数组。

int tester(vector<Item *> &s)

(作为参考传递,以避免复制或需要修改)

您还需要修改 tester 函数内的代码,以便作为向量正常工作。

A vector is not an array.

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.

酒绊 2024-12-15 15:59:42

您应该首先修复

test.h:5: error: âstringâ does not name a type

,可能是通过 using namespace std;#include

You should fix

test.h:5: error: âstringâ does not name a type

first, probably by using namespace std; and #include <string>

意中人 2024-12-15 15:59:42

您缺少包含内容

#include <string>
#include <vector>

,需要使用 std::stringstd::vectorstd::vector 不是数组,因此您应该将向量作为引用传递

int tester(std::vector<Item*> & vec) { //... }

,甚至作为 const std::vector传递。 & 如果您不打算修改传递的向量。

另外,您确定需要一个指针向量吗?你想达到什么目的?

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

int tester(std::vector<Item*> & vec) { //... }

or 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?

幽蝶幻影 2024-12-15 15:59:41

std::vectorT* [] 不是兼容的类型。

按如下方式更改您的 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;
}

您可以通过多种方式传递此 std::vector,并且所有方式的含义都略有不同:

// 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*>*);

A std::vector<T> and T* [] are not compatible types.

Change your tester() function signature as follows:

//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:

// 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*>*);
尤怨 2024-12-15 15:59:41

将其传递为 std::vector; & (对向量的引用)并使用迭代器对其进行迭代。

Pass it as std::vector<Item *> & (reference to vector) and use iterator to iterate through it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文