compile error with multiple source files and a header file

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
I think your problem is that you are using #define _VECTOR_ before calling #include <vector> .

In <vector> we have this:
1
2
3
4
5
#ifndef _VECTOR_
#define _VECTOR_
... the entire header

#endif 


This means that by manually defining _VECTOR_, you are effectively skipping the entire header. Since the vector header has this #ifndef stuff, it means that it has already included protections against being included twice. This means that you don't have to do the protection yourself.

Remove your pre-processor logic and I think you'll find that it will work.
Here are some other tips:

1. Use std:: tags in your header. You use using namespace std in one of your cpp files, but this doesn't affect the other cpp files or the header.
2. You need to #include fun22.h in both source files. Otherwise they won't be linked.
3. You don't need to do this for your specific example, but it is common practice to add some protections to prevent your header from being included multiple times. You can use #pragma once or
1
2
3
4
#ifndef FUN22_H
#define FUN22_H
... Your header here
#endif 
Hi Stewbond,

thanks for your reply!
for 1). In fact, I only know that I need to add std:: before ostream like std::ostream in the my header file, but does vector<int> need so? like std::vector<int>

for 2). I used to add #include fun22.h in both source files too. But the problem still not solved.

Now my code is look like this:
1
2
3
4
5
6
7
8
9
//in main.cpp
#ifndef FUN22_H
#define FUN22_H
#include<vector>
#include<iostream>
#include "fun22.h"
#endif

int main(){ ....


1
2
3
4
5
//in func22.h
#include<vector>

bool calc_elems(vector<int>& , int);
void display_elems(vector<int>& , string& , std::ostream& );


1
2
3
4
5
6
7
8
9
10
//in fun22.cpp
#ifndef FUN22_H
#define FUN22_H
#include<vector>
#include<iostream>
#include "fun22.h"
#endif

bool calc_elems(vector<int> &v, int pos){
...
Topic archived. No new replies allowed.