0

I'm trying to pass a vector to to a function as a reference so that I can print the contents. The problem is the following compiling errors.

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) {
                          ^~~

I believe the problem could possibly be related to the way I have my files set up. From all the research I have done I can't find anything that has helped me other than to #include in the header file. Which I thought was bad practice, but I understand I could be wrong.

[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;
}

So my question is am I passing the reference incorrectly? or have I set up the Header file incorrectly?

Thanks

cmehmen
  • 249
  • 1
  • 3
  • 12
  • 2
    Stop using `using namespace std;`, prefix `std::` for whatever you use from the standard library instead. – πάντα ῥεῖ Jan 16 '19 at 22:25
  • 5
    You didn't `#include ` in your header. Also you should write `std::vector` – UnholySheep Jan 16 '19 at 22:25
  • `using namespace std;` is what is getting you into trouble, Don't do it. use the qualified name like `std::vector`,, especially in headers. – lakeweb Jan 16 '19 at 22:25
  • 4
    @lakeweb — you’re right that `using namespace std;` is a bad idea, but it’s not the problem here. – Pete Becker Jan 16 '19 at 22:31
  • Yes it is. You are ' using namespace std;` after you include Print.h. Hence, the error. – lakeweb Jan 16 '19 at 22:35
  • @lakeweb it's not the comlete problem, after updating my code - removing namespace and using std:: in the correct spots I get the following -- error: variable or field ‘print_stuff’ declared void (and) error: ‘vector’ is not a member of ‘std’ – cmehmen Jan 16 '19 at 22:38
  • 1
    Did you `std::vector` throughout your header? I can't see your updated code. – lakeweb Jan 16 '19 at 22:42
  • @lakeweb just added updated code – cmehmen Jan 16 '19 at 22:47
  • 1
    print.cpp could use a `#include ` – user4581301 Jan 16 '19 at 22:49
  • @user4581301 That fixed the problem. From my understanding of how things were compiled i wasn't aware that I had to #include , , etc for each individual file. Now it does make more sense though. Thank you! – cmehmen Jan 16 '19 at 22:57
  • @cmehmen "*error: ‘vector’ is not a member of ‘std’*" - that happens when you are missing `#include `. You can't use something that is not declared. – Remy Lebeau Jan 16 '19 at 23:14

2 Answers2

1

To solve the problem I had to

List item

  1. add #include <vector> to the Print.h file
  2. add #include <vector> and #include <iostream> to Print.cpp
  3. pass reference as void print_stuff(std::vector<int> &month_mileage);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
cmehmen
  • 249
  • 1
  • 3
  • 12
  • 2
    It would be more typical, and essentially required once you start getting into class definitions, to `#include "Print.h"` in Print.cpp. (Then it doesn't need `` since it got that via Print.h, but it still needs ``.) – aschepler Jan 16 '19 at 23:14
  • @aschelper This helped me a lot. So would it be bad practice to simplify everything and just have `#include ` and `#include ` in `"Print.h"` and then just have #include `"Print.h"` in both `"Print.cpp"` and `"main.cpp"` ? – cmehmen Jan 16 '19 at 23:32
  • @cmehmen typically no. A header should `#include` what it needs, no more and definitely no less. Everything you include has cost, be it loading unnecessary files (that some other file needed) at compile time or debugging and refactoring because the brain now has to consider extra avenues suggested by unnecessary header includes. – user4581301 Jan 16 '19 at 23:53
  • Sometimes you will find going even lighter can help. Say you have a reference to `MassiveClass` in a header. Including all of MassiveHeader.h to get `MassiveClass` can be avoided by forward declaring `MassiveClass` in the header to satisfy the reference. You'll see this in the standard library sometimes. In GCC you can get weird *`stringstream` not fully defined* messages because iostream forward declares `stringstream` rather than pull in a header that relatively few users of iostream need. The same sometimes happens with `string`. – user4581301 Jan 16 '19 at 23:57
-3

Note that unlike C arrays, c++ classes have to have & before them just like regular parameters you pass by, otherwise it will create a copy on the stack in the function it was passed to.

#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.
}
Prekzursil
  • 11
  • 2
  • So you are telling me to add '&; before the variable name that I pass to function, which I already did. And then post code that is absent of what you said, and never even calls the function? – cmehmen Jan 16 '19 at 23:05
  • Yes I forgot to call it, but yes basically &v means that after running the function the vector will be affected but if not it will make a copy of it and therefore it will not be affected after the end of the function. – Prekzursil Jan 16 '19 at 23:08
  • Mind you, if you use anything else then gcc like visual studio, it will not accept the custom made bits library, so you will have to add all libraries you use or make a custom one yourself. – Prekzursil Jan 16 '19 at 23:09
  • 3
    It's right in the original code: `void print_stuff(vector &month_mileage);`. Suggesting adding a `&` to the function parameter is not useful when it's already there. Also, `#include ` is an even worse idea than `using namespace std;`. – aschepler Jan 16 '19 at 23:12
  • Next time actually look at the code that is posted, and then read the question. what you are telling me to do was already implemented in my original code. – cmehmen Jan 16 '19 at 23:12
  • 2
    Unrelated: By themselves [`#include`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) can lead you into a lot of trouble. Together they can combine into a terrifying, program-destroying monster. Exercise caution when using them in your code and avoid using either of them in an answer. – user4581301 Jan 16 '19 at 23:14