将向量作为参考传递的正确方法是什么?

时间:2019-01-16 22:21:39

标签: c++

我正在尝试将向量传递给函数作为参考,以便可以打印内容。问题是以下编译错误。

Print.h:7:19: error: variable or field ‘print_stuff’ declared void
  void print_stuff(vector<int> &month_mileage);
                   ^~~~~~
Print.h:7:19: error: ‘vector’ was not declared in this scope
Print.h:7:26: error: expected primary-expression before ‘int’
  void print_stuff(vector<int> &month_mileage);
main.cpp: In function ‘int main()’:
main.cpp:20:12: error: ‘print_stuff’ is not a member of ‘Print’
     Print::print_stuff(&month_mileage);
            ^~~~~~~~~~~
Print.cpp:4:19: error: variable or field ‘print_stuff’ declared void
  void print_stuff(vector<int> &month_mileage) {
                   ^~~~~~
Print.cpp:4:19: error: ‘vector’ was not declared in this scope
Print.cpp:4:26: error: expected primary-expression before ‘int’
   void print_stuff(vector<int> &month_mileage) {
                          ^~~

我认为问题可能与我设置文件的方式有关。根据我所做的所有研究,除了在头文件中包含#include之外,我找不到其他对我有帮助的东西。我以为是不好的做法,但我知道我可能是错的。

[main.cpp] 
 #include "Print.h"
 #include <vector>
 #include <iostream>

 using namespace std;
 using namespace Print;

 int main() {

    int num;
    vector<int> month_mileage(0,12);

     cout << " Please enter your mileage for the past 12 months\n";

     for( int i = 0; i < 12; i++) {
         cin >> num;
         month_mileage[i] = num;
     }

     Print::print_stuff(month_mileage);


 return 0;
 }
[Print.h]---------------------------------------------------
  0 #ifndef PRINT_H
 #define PRINT_H


 namespace Print {

         void print_stuff(vector<int> &month_mileage);

 };
 #endif
[Print.cpp]---------------------------------------------------
 namespace Print{


         void print_stuff(vector<int> &month_mileage) {

             for(int i = 0; i < 12; i++) {
                     for(int a = 0; a < &month_mileage[i]; a++) {
                             std::cout <<"|";
                     }//END FOR     
                 std::cout <<'\n';
             }//END OUTER FOR
         }//END PRINT_STUFF

 }


UPDATED CODE is as follows

[Print.h]--------------------------------------------------- 
#ifndef PRINT_H
#define PRINT_H
#include <vector>

namespace Print {

    void print_stuff(std::vector<int> &month_mileage);

};
#endif

[Print.cpp]---------------------------------------------------

namespace Print{


    void print_stuff(std::vector<int> &month_mileage) {

        for(int i = 0; i < 12; i++) {
            for(int a = 0; a < month_mileage[i]; a++) {
                std::cout <<"|";
            }   
            std::cout <<'\n';
        }
    }

}
[main.cpp]---------------------------------------------------
#include "Print.h"
#include <vector>
#include <iostream>

using namespace Print;

int main() {

   int num;
   std::vector<int> month_mileage(0,12);

   std::cout << " Please enter your mileage for the past 12 months\n";

    for( int i = 0; i < 12; i++) {
        std::cin >> num;
        month_mileage[i] = num;
    }

    Print::print_stuff(month_mileage);


return 0;
}

所以我的问题是我是否正确传递了引用?还是我没有正确设置头文件?

谢谢

2 个答案:

答案 0 :(得分:1)

要解决我必须解决的问题

列表项

  1. #include <vector>添加到Print.h文件
  2. #include <vector>#include <iostream>添加到Print.cpp
  3. 将引用传递为void print_stuff(std::vector<int> &month_mileage);

答案 1 :(得分:-3)

请注意,与C数组不同,c ++类必须像传递的常规参数之前具有&,否则,它将在传递给它的函数中在堆栈上创建一个副本。

#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> v)
{
    //your code here
}

int main()
{
    vector<int> v;
    int n,x;
    cin>>n;enter code here
    for(int i=1;i<=n;i++)
        v.push_back(x);
    cout<<fun(v); // make it void if you need it void.
}