传递向量到函数c ++ [英] passing vector to function c++

查看:134
本文介绍了传递向量到函数c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个main.cpp test.h和test.cpp>我试图传递我的向量通过所以我可以使用它在test.cpp但我不断收到错误。

  // file:main.cpp 
int main(){
vector< Item *> s;
//加载我的文件并赋值s [i] - > name和s [i] - 地址
tester;
}

// 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 cout<< s [i] - > name<<<< s [i] - > address<< endl;
}
return 0;
}



---------------错误--------
文件包括从main.cpp:13:
test.h:5:错误:âstringâ不命名类型
test.h:6:错误:âstringâ没有命名类型
main .cpp:在函数âptmain()â:
main.cpp:28:error:不能转换âstd:: vector< Item *,std :: allocator< Item *> >âtoâ¨Item**â¨for for for()))))â>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>解决方案

A std :: vector< T> T * []



更改 tester()函数签名如下:

  // file:test.cpp 
int tester(const std :: vector< Item>& s) std :: vector
//因为你不需要改变
//在这个函数中的值
{
for(size_t i = 0; i cout< s [i] - > name<<<< s [i] - > address<< endl;
}
return 0;
}

有几种方法可以传递此 std: :vector< T> 并且都有稍微不同的含义:

 的向量
//,这将是本函数的范围
void tester(std :: vector< Item *>);

//这将使用向量的引用
//此引用可以在
//测试器函数中修改
//这不涉及向量的第二个副本
void tester(std :: vector< Item *>&);

//这将使用向量的const引用
//此引用不能在
//测试器函数中修改
//这是不涉及向量的第二个副本
void tester(const std :: vector< Item *>&);

//这将使用向量的指针
//这不涉及向量的第二个副本
// caveat:使用裸指针可能是危险的,
//如果可能的话,应该避免非平凡的情况
void tester(std :: vector< 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**)â

解决方案

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

这篇关于传递向量到函数c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆