Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Complex number question
Forum Updated to NodeBB v4.3 + New Features

Complex number question

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ad5xj
    wrote on last edited by
    #1

    I have an application that uses complex numbers. From an example I have clipped some supposedly working code that includes a complex number definition:

    @
    double re;
    double im;
    complex(double r = 0.0, double i = 0.0) : re(r), im(i) { }
    @

    yet I can find no documentation to explain this to me. This may be a newbie question but what does the colon after the complex number do? Qt complains that line 3 does not name a type. I thought complex WAS a type when <complex> is included?!

    What am I missing / doing wrong?

    Ken AD5XJ

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      This is a basic C++ question. The : in line 3 above initializes the re and the im variables to r and i respectively on construction of complex.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ad5xj
        wrote on last edited by
        #3

        Why then does the compiler complain about it not having a type?

        Ken AD5XJ

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JohnKaul
          wrote on last edited by
          #4

          FWIW here is a quick example/addition to what Andre said.

          @
          class base
          {
          public:
          virtual ~base() {}
          };

          class derived : public base
          {
          unsigned Value;
          public:
          derived(unsigned val) : Value(val) {}
          };

          int main(int argc, const char *argv[])
          {
          derived myDerived(0);

          return 0;
          

          }
          @

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shahid.pk
            wrote on last edited by
            #5

            bq.
            double re;
            double im;
            complex(double r = 0.0, double i = 0.0) : re(r), im(i) { }

            the compiler is complaining because what you are doing is conflicting with what the compiler do automatically.It is called the defaulf constructor and it should be provided with no arguments.

            try this instead

            @
            double re;
            double im;
            complex() : re(0.0), im(0.0) { }
            complex(double r ,double i) : re(r),im(i) {}
            @

            If you want to initialize you variables to 0 default constructor is the correct way.I guess you were trying to do just that.and you can also do it like this

            @
            double re;
            double im;
            complex() {re=0.0;im=0.0; }
            complex (double r, double i) {re=r;im=i;}
            @

            Love to learn....

            1 Reply Last reply
            0
            • A Offline
              A Offline
              ad5xj
              wrote on last edited by
              #6

              Found it! The example I clipped the code from used ambigous terms calling the redefinition the same as the class. So I renamed the new one m_complex every where it is used and all is well with the compiler.

              Ken AD5XJ

              1 Reply Last reply
              0
              • F Offline
                F Offline
                foxyz
                wrote on last edited by
                #7

                your code just like a class definition
                3.complex(double r = 0.0, double i = 0.0) : re( r ), im(i) { }
                this is a constructor of class complex,so if you want to use class complex,just declare it:
                complex mycomplex;
                or complex mycomplex(0.12, 0.56);

                I just know coding and coding

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved