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