C++ 打印C+中多个向量的第一个值+;

C++ 打印C+中多个向量的第一个值+;,c++,vector,C++,Vector,我试图打印主函数中显示的每个向量的第一个值 #include <iomanip> #include <map> using namespace std; typedef unsigned int vect; int main() { std::vector<vect> p; vector<vect> a = { 4,2,3,1 }; vector<vect> b = { 4,2,3,1 }; ve

我试图打印主函数中显示的每个向量的第一个值

#include <iomanip>
#include <map>

using namespace std;

typedef unsigned int vect;

int main() {
    std::vector<vect> p;

    vector<vect> a = { 4,2,3,1 };
    vector<vect> b = { 4,2,3,1 };
    vector<vect> c = { 4,2,3,1 };
    vector<vect> d = { 4,2,3,1 };

    int i;
    for (i=0; i<a.size(); i++)
    cout << a[i];
}
#包括
#包括
使用名称空间std;
typedef无符号整数向量;
int main(){
std::向量p;
向量a={4,2,3,1};
向量b={4,2,3,1};
向量c={4,2,3,1};
向量d={4,2,3,1};
int i;
对于(i=0;i
我想从
main()
调用函数
first\u preference()
,函数应该在这里打印每个向量的第一个值

一些问题:

  • 您的头文件中有一个全局
    std::vector p
    (这不是一个好主意)它被
    main
    中的
    std::vector p
    隐藏。您在
    main
    中的
    p
    中输入的内容将无法从
    test
    的实例访问。这些实例只知道全局
    p

  • 您没有在
    main.cpp
    中包含“function.h”
,因此无法在
main
中创建
test
对象

  • 如果您在
    main.cpp
    中包含“function.h”,则无需
    typedef unsigned int vect;
    ,因为您已经在
    function.h
    中这样做了。这不是一个错误,而是混淆和不必要的

  • vector
    实例
    a、b、c
    d
    test
    或任何
    p
    都没有任何联系,因此您在这些向量中输入的内容不可能由
    test
    实例打印,除非您以某种方式将它们传递给
    test

  • 声明
    vect
    的向量,但
    first_preference()
    按值返回
    person
    vect
    person
    恰好是相同基本类型的别名,但此接口似乎有问题

  • main.cpp
    中,您不实例化
    测试
    ,而是迭代
    a
    ,并且从未调用
    first_preference()
    ,因此不希望使用它

  • 我想从
    main()
    调用函数
    first\u preference()
    ,函数应该在这里打印每个向量的第一个值

    一些问题:

    • 您的头文件中有一个全局
      std::vector p
      (这不是一个好主意)它被
      main
      中的
      std::vector p
      隐藏。您在
      main
      中的
      p
      中输入的内容将无法从
      test
      的实例访问。这些实例只知道全局
      p

    • 您没有在
      main.cpp
      中包含“function.h”,因此无法在
      main
      中创建
      test
      对象

    • 如果您在
      main.cpp
      中包含“function.h”,则无需
      typedef unsigned int vect;
      ,因为您已经在
      function.h
      中这样做了。这不是一个错误,而是混淆和不必要的

    • vector
      实例
      a、b、c
      d
      test
      或任何
      p
      都没有任何联系,因此您在这些向量中输入的内容不可能由
      test
      实例打印,除非您以某种方式将它们传递给
      test

    • 声明
      vect
      的向量,但
      first_preference()
      按值返回
      person
      vect
      person
      恰好是相同基本类型的别名,但此接口似乎有问题

    • main.cpp
      中,您不实例化
      测试
      ,而是迭代
      a
      ,并且从未调用
      first_preference()
      ,因此不希望使用它


    投票以键入错误的形式关闭。
    投票::first_preference
    是成员函数的地址,而不是对它的调用。函数如何调用?
    实例。成员函数(参数)
    是一种方法。将您的代码变成一个,这样会更容易帮助。@TEDLYNMO我已经简化了问题,希望它会更容易帮助?我正在尝试按您建议的方式调用函数,但它不会使您过于简化。您的简化代码没有
    test
    对象,因此
    test
    的非静态成员无法使用调用。投票以键入方式关闭。
    &vote::first\u preference
    是成员函数的地址,而不是对它的调用。函数如何调用?
    实例。成员函数(参数)
    是一种方法。将您的代码变成一个,这样会更容易帮助。@TEDLYNMO我已经简化了问题,希望它会更容易帮助?我正在尝试按您建议的方式调用函数,但它不会使您过于简化。您的简化代码没有
    test
    对象,因此
    test
    的非静态成员无法使用打电话。
    #include "function.h"
    #include <string>
    #include <iostream>
    
    using namespace std;
    
     person test::first_preference() const {
    
            const person& first = p.front();
            return first; //current first pref
        }
    
    #ifndef FUNCTION_H
    #define FUCNTION_H
    
    #include <vector>
    
    typedef unsigned int person;
    typedef unsigned int vect;
    
    std::vector<vect> p;
    
    
    class test {
    
    public:
        person first_preference() const;
    };
    
    #endif