compile error after change int to complex

Dear c++ expert:(I am using g++4.6.1)
I have the following code to do some math
and I like to change from int type to complex type
-----------------------------------------------------
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "matrix.hpp" // recipe 11.13

#include "kmatrix.hpp" // recipe 11.14

#include <iostream>

#include <cassert>



using namespace std;



template<class M1, class M2, class M3>

void matrixMultiply(const M1& m1, const M2& m2, M3& m3)

{

  assert(m1.cols( ) == m2.rows( ));

  assert(m1.rows( ) == m3.rows( ));

  assert(m2.cols( ) == m3.cols( ));

  for (int i=m1.rows( )-1; i >= 0; --i) {

    for (int j=m2.cols( )-1; j >= 0; --j) {

      for (int k = m1.cols( )-1; k >= 0; --k) {

        m3[i][j] += m1[i][k] * m2[k][j];

      }

    }

  }

}



int main( )

{

  matrix<int> m1(2, 1);

  matrix<int> m2(1, 2);

  kmatrix<int, 2, 2> m3;

  m3 = 0;

  m1[0][0] = 1; m1[1][0] = 2;

  m2[0][0] = 3; m2[0][1] = 4;

  matrixMultiply(m1, m2, m3);

  cout << "(" << m3[0][0] << ", " << m3[0][1] << ")" << endl;

  cout << "(" << m3[1][0] << ", " << m3[1][1] << ")" << endl;

}

-------------------------------------------------------------
in addition to change all "int" to "complex" what else I should do?
I get compile error
-----------
1
2
3
4
5
6
root@eric-laptop:/home/eric/cppcookbook/ch11# g++  Example11-32.cpp 
Example11-32.cpp: In function ‘int main()’:
Example11-32.cpp:28:18: error: template argument 1 is invalid
Example11-32.cpp:28:22: error: invalid type in declaration before ‘(’ token
Example11-32.cpp:28:27: warning: expression list treated as compound expression in initializer [-fpermissive]
Example11-32.cpp:29:18: error: template argument 1 is invalid

------------
plz help, thank your help a lot in advance, Eric
Last edited on
If that was working code and you replaced "int" with "complex", notice, that complex is a template class. It should be "complex<double>" or whatever you need.
thanks your reply. Now it can compile but, it did not run correct (or as expected). for example, I set m2[0][0] =
1
2
3
4
5
6
7
8
9
   matrix<std::complex<double> > m1(2, 1);
   matrix<std::complex<double> > m2(1, 2);
   kmatrix<std::complex<double> , 2, 2> m3;
   m3 = 0;
   m1[0][0] = (1.1, 1.1); m1[1][0] = (2.2, 2.2);
   m2[0][0] = (3.1, 3.3); m2[0][1] = (4.4, 4.4);

   cout << "m1[00] = " << m1[0][0] << endl;
   cout << "m2[00] = " << m2[0][0] << endl;

-----------------------
but the run result is
1
2
3
4
m1[00] = (1.1,0)
m2[00] = (3.3,0)
m1[00] + m2[00] = (4.4,0)

-------------------------------------
my complex number did not be correctly recognized
only imaginary part be accepted as real part and
real part be truncate and
adaptive imaginary part always be 0
it looks
my complex number be treated as input of stack at that overloaded [][] dual index
I suspect my template "std::complex<double>" is really be propogated correctly to matrix class(and its subsequence valarray).
you can see the code I use from
http://examples.oreilly.com/9780596007614/
11-32.cpp of c++ cookbook
this is my matrix.hpp file

--------------------------
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef MATRIX_HPP
#define MATRIX_HPP

#include "stride_iter.hpp" // see Recipe 11.12
#include <valarray>
#include <numeric>
#include <algorithm>

template<class Value_T>
class matrix
{
public:
  // public typedefs
  typedef Value_T value_type;
  typedef matrix self;
  typedef value_type* iterator;
  typedef const value_type* const_iterator;
  typedef Value_T* row_type;
  typedef stride_iter<value_type*> col_type;
  typedef const value_type* const_row_type;
  typedef stride_iter<const value_type*> const_col_type;

  // constructors
  matrix() : nrows(0), ncols(0), m() {}
  matrix(int r, int c) : nrows(r), ncols(c), m(r * c) {}
  matrix(const self& x) : m(x.m), nrows(x.nrows), ncols(x.ncols) {}

  template<typename T>
  explicit matrix(const std::valarray<T>& x)
  : m(x.size() + 1), nrows(x.size()), ncols(1)
  {
    for (int i=0; i<x.size(); ++i) m[i] = x[i];
  }
  // allow construction from matrices of other types
  template<typename T>
  explicit matrix(const matrix<T>& x)
  : m(x.size() + 1), nrows(x.nrows), ncols(x.ncols)
  {
     copy(x.begin(), x.end(), m.begin());
  }

  // public functions
  int rows() const { return nrows; }
  int cols() const { return ncols; }
  int size() const { return nrows * ncols; }

   // element access
   row_type row_begin(int n) { return &m[n * cols()]; }
   row_type row_end(int n) { return row_begin() + cols(); }
   col_type col_begin(int n) { return col_type(&m[n], cols()); }
   col_type col_end(int n) { return col_begin(n) + cols(); }
   const_row_type row_begin(int n) const { return &m[n * cols()]; }
   const_row_type row_end(int n) const { return row_begin() + cols(); }
   const_col_type col_begin(int n) const { return col_type(&m[n], cols()); }
   const_col_type col_end(int n) const { return col_begin() + cols(); }
   iterator begin() { return &m[0]; }
   iterator end() { return begin() + size(); }
   const_iterator begin() const { return &m[0]; }
   const_iterator end() const { return begin() + size(); }

   // operators
   self& operator=(const self& x) {
     m = x.m;  nrows = x.nrows;  ncols = x.ncols; return *this;
   }
   self& operator=(value_type x) { m = x; return *this; }
   row_type operator[](int n) { return row_begin(n); }
   const_row_type operator[](int n) const { return row_begin(n); }
   self& operator+=(const self& x) { m += x.m; return *this; }
   self& operator-=(const self& x) { m -= x.m; return *this; }
   self& operator+=(value_type x) {m += x; return *this; }
   self& operator-=(value_type x) { m -= x; return *this; }
   self& operator*=(value_type x) { m *= x; return *this; }
   self& operator/=(value_type x) { m /= x; return *this; }
   self& operator%=(value_type x) { m %= x; return *this; }
   self operator-() { return -m; }
   self operator+() { return +m; }
   self operator!() { return !m; }
   self operator~() { return ~m; }

   // friend operators
   friend self operator+(const self& x, const self& y) { return self(x) += y;}
   friend self operator-(const self& x, const self& y) { return self(x) -= y; }
   friend self operator+(const self& x, value_type y) { return self(x) += y; }
   friend self operator-(const self& x, value_type y) { return self(x) -= y; }
   friend self operator*(const self& x, value_type y) { return self(x) *= y; }
   friend self operator/(const self& x, value_type y) { return self(x) /= y; }
   friend self operator%(const self& x, value_type y) { return self(x) %= y; }
  private:
    mutable std::valarray<Value_T> m;
    int nrows;
    int ncols;
};

#endif 


need help again and thank a lot in advance, Eric

You can't define complex numbers with (r,i) syntax. In C++, "," operator ignores the value of the first operand and returns the second one. It's a bit like ";". (Note that the "," in function argument lists is not the same one)

To create a complex number, you need to call the constructor of complex: m[0][0] = std::complex<double>( 2, 2 );. It might be a good idea to #define C std::complex<double> to make it easier to read/write.
thanks
Topic archived. No new replies allowed.