I want to separate some functions from the main function source file, but I find that I cannot compile the program after doing the separation, can someone tell me what the problem is?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
// my function.cpp (named fun22.cpp) which store the declaration of functions that I separate from the main source file
#ifndef _VECTOR_
#define _VECTOR_
#include<vector>
#endif
#ifndef _IOSTREAM_
#define _IOSTREAM_
#include<iostream>
#endif
using namespace std;
bool calc_elems(vector<int> &v, int pos){
if(pos<=0 || pos>64){
cerr << "Sorry. Invalid range: " << pos << endl;
return false;
}
for(int i=v.size()+1;i<=pos;i++){
v.push_back(i*(3*i-1)/2);
}
return true;
}
void display_elems(vector<int> &v, string &title, ostream &os = cout){
os << title << endl;
int len = v.size();
for(int i=0;i<len;i++)
os << v[i] << ' ';
os << endl;
}
|
1 2 3 4 5 6 7 8
|
// header file (named fun22.h)
#ifndef _VECTOR_
#define _VECTOR_
#include<vector>
#endif
bool calc_elems(vector<int>& , int);
void display_elems(vector<int>& , string& , ostream& );
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
//source file of main function (named main.cpp)
#ifndef _VECTOR_
#define _VECTOR_
#include<vector>
#endif
#ifndef _IOSTREAM_
#define _IOSTREAM_
#include<iostream>
#endif
#include "fun22.h"
int main(){
vector<int> pent;
String title = "Pentagonal Numeric Series";
if(calc_elems(pent,0))
display_elems(pent,title);
if(calc_elems(pent,14))
display_elems(pent,title);
return 0;
}
|
I try 'gcc fun22.cpp main22.cpp', I get the following error message:
In file included from main22.cpp:11:
fun22.h:6: error: ‘vector’ was not declared in this scope
fun22.h:6: error: expected primary-expression before ‘int’
fun22.h:6: error: expected primary-expression before ‘int’
fun22.h:6: error: initializer expression list treated as compound expression
fun22.h:7: error: variable or field ‘display_elems’ declared void
fun22.h:7: error: ‘vector’ was not declared in this scope
fun22.h:7: error: expected primary-expression before ‘int’
fun22.h:7: error: ‘string’ was not declared in this scope
fun22.h:7: error: expected primary-expression before ‘,’ token
fun22.h:7: error: ‘ostream’ was not declared in this scope
fun22.h:7: error: expected primary-expression before ‘)’ token
main22.cpp: In function ‘int main()’:
main22.cpp:14: error: ‘vector’ was not declared in this scope
main22.cpp:14: error: expected primary-expression before ‘int’
main22.cpp:14: error: expected ‘;’ before ‘int’
main22.cpp:15: error: ‘String’ was not declared in this scope
main22.cpp:15: error: expected ‘;’ before ‘title’
main22.cpp:17: error: ‘pent’ was not declared in this scope
main22.cpp:17: error: ‘calc_elems’ cannot be used as a function
main22.cpp:18: error: ‘title’ was not declared in this scope
main22.cpp:18: error: ‘display_elems’ was not declared in this scope
main22.cpp:19: error: ‘pent’ was not declared in this scope
main22.cpp:19: error: ‘calc_elems’ cannot be used as a function
main22.cpp:20: error: ‘title’ was not declared in this scope
main22.cpp:20: error: ‘display_elems’ was not declared in this scope