Data Type Problem with Different GCC version

Dear all,

I have the following code, that
uses some arithmetic in 'id2tagnum' function:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>      
#include <vector>        
#include <cmath>         
#include <map>
using namespace std;

template <typename T> void  prn_vec(std::vector < T >&arg, string sep="") 
{
    for (int n = 0; n < arg.size(); n++) { 
        cout << arg[n] << sep;    
    }


}

vector <int> id2tagnum(int id, int tgl ) {

    // replicate
    vector <int> Rep;    
    Rep.assign(tgl, id-1);    
    vector <int> nuMTag; 

    for ( int i=tgl-1; i>=0  ; i-- ) {
        double pw = pow(4.00, double (i));
        int fm  = double(Rep[i])/pw;
        int md  = fm % 4;
        nuMTag.push_back(md);     
    }

    return nuMTag;

}

int main () {
    int tagLength = 5;

    for ( int i=1; i<=10; i++ ) {
          cout << i << "\t";
          vector <int> Result = id2tagnum(i,tagLength);
          prn_vec<int>(Result);

    }




}

It works fine in:

 
gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)


But it doesn't work in:

 
gcc (GCC) 3.3.3 (SuSE Linux)


yielding this error:
1
2
3
4
5
6
7
8
9
10
11
12
test.cpp: In function `std::vector<int, std::allocator<int> > id2tagnum(int, 
   int)':
test.cpp:29: warning: initialization to `int' from `double'
test.cpp:29: warning: argument to `int' from `double'
test.cpp: In function `std::vector<char, std::allocator<char> > 
   tagnum2acgt(std::vector<int, std::allocator<int> >)':
test.cpp:43: error: `i' undeclared (first use this function)
test.cpp:43: error: (Each undeclared identifier is reported only once for each 
   function it appears in.)
test.cpp:43: error: `w' undeclared (first use this function)



How can I resolve the problem so that it can compile in all GCC version?

It should yield this result:
1
2
3
4
5
6
7
8
9
10
11
1	00000	
2	00001	
3	00002	
4	00003	
5	00010	
6	00011	
7	00012	
8	00013	
9	00020	
10	00021	
Last edited on
In function tagnum2acgt? Are you sure you're compiling the same source?

It's obviously seen a function:
std::vector<char> tagnum2acgt(std::vector<int>)
closed account (z05DSL3A)
I would first address the warning (just incase there is some nock on effect).

change line 28 to
int fm = int (double(Rep[i])/pw);

Some compilers may issue a signed/unsigned mismatch warning on line 12, try:
for (unsigned int n = 0; n < arg.size(); n++) {
Safer is to use boost::numeric_cast<>:

 
int fm = boost::numeric_cast<int>( Rep[i] / pw );


which will throw if the conversion fails due to the fact that double can represent numbers larger than int can hold.
Topic archived. No new replies allowed.