Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved] Using C++ class in projects
Qt 6.11 is out! See what's new in the release blog

[Solved] Using C++ class in projects

Scheduled Pinned Locked Moved Mobile and Embedded
11 Posts 3 Posters 5.7k 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.
  • M Offline
    M Offline
    mlong
    wrote on last edited by
    #2

    Are you wrapping your class definition properly with the namespace?

    .h:
    @
    namespace Useful {

    class Foo {
    Foo();
    ...
    };

    }
    @

    .cpp:
    @
    namespace Useful {

    Foo::Foo() { ... };

    }
    @

    Software Engineer
    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pascal95abc
      wrote on last edited by
      #3

      No, I don't use namespaces either in my header file or in my cpp file.
      I want to make it as easy as it can be done.
      So, this is the content of my Useful file:
      #include "useful.h"

      This is my header:
      @#ifndef USEFUL_H
      #define USEFUL_H

      #include <QWidget>

      class Useful : public QWidget
      {
      Q_OBJECT
      public:
      explicit Useful(QWidget *parent = 0);
      double sqr(double x);

      signals:

      public slots:
      };

      #endif // USEFUL_H@

      and my cpp
      @#include "useful.h"

      Useful::Useful(QWidget *parent) :
      QWidget(parent)
      {
      }

      double sqr(double x)
      {
      return(x * x);
      }@

      So, I just have the sqr function to simplify this thread.

      I want to use sqr(4) instead of Useful.sqr(4) in my main/mainwindow cpp.

      Qt 4.7.3
      Symbian OS v9.4 / Symbian^1, S60 5th Edition
      on N97

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lycis
        wrote on last edited by
        #4

        As you are wrapping your function inside a class you'll need to call it by using that class. If you only want your function to be inside the namespace you don't need a class, but only wrap this function inside the namespace.

        useful.h:
        @
        namespace useful{
        double sqr(double x)
        }
        @

        useful.cpp:
        @
        double useful::sqr(double x)
        {
        return (x*x);
        }
        @

        In your main code you may now use either

        @
        using namespace useful;
        ...
        double foo = sqr(2.0);
        ...
        @

        or

        @
        ...
        double foo = useful::sqr(2.0);
        ...
        @

        If you wrap your function inside a class that's inside a namespace it works the same regarding the namespace usage, but you'll have to instantiate a class for calling the function or declare the function to be static.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pascal95abc
          wrote on last edited by
          #5

          Thanks for the fast answer!

          Do I have to set the namespace's name to "useful", i.e. lowercase ?
          or should this be
          @namespace Useful { ...@
          ?

          And does the whole class useful (class Useful : public QWidget) in the header file be wrapped with "namespace ..." or what do I actually have to wrap with the namespace?

          Qt 4.7.3
          Symbian OS v9.4 / Symbian^1, S60 5th Edition
          on N97

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lycis
            wrote on last edited by
            #6

            It does not matter if your namespace is lower or upper case. I wrote it in lower case because I prefer coding conventions where upper case is only applied to classes and namespaces are lower case so you'll if you are using a static class function or a namespace function.

            If you are encapsulating your function inside a class (called Useful) that's inside a namespace you'll have to do something like:

            @
            using namespace MyNamespace;

            // if the function is not static
            Useful u;
            double d = u.sqrt(2.0);

            // if the function is static
            double d = Useful::sqrt(2.0);
            @

            If you're not using namespace :

            @
            // function is non-static
            MyNamespace::Useful u;
            double d = u.sqrt(2.0);

            // function is static
            double d = MyNamespace::Useful::sqrt(2.0);
            @

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pascal95abc
              wrote on last edited by
              #7

              Actually, I want to use namespace to avoid using these long codes to call a simple function like my sqr one.
              Is there nothing simple so that I can easily call my function with
              @sqr(1.234);@ ?

              And you can refer to my code posted "here":http://qt-project.org/forums/viewreply/83941/.

              btw: I think line 8 should be
              @double d = Useful::sqr(2.0);@ ?

              Qt 4.7.3
              Symbian OS v9.4 / Symbian^1, S60 5th Edition
              on N97

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lycis
                wrote on last edited by
                #8

                In that case you should not wrap your function inside a class, but only wrap it inside the namespace. In that case refer to my first post for the definitions. You just need to define the function, but not in a class :)

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pascal95abc
                  wrote on last edited by
                  #9

                  Ok, but what to write in the corresponding files in addition ?
                  The code as is provided by you is clear but not sufficient to be placed in a file.
                  Sorry, I have no idea what to add.

                  Qt 4.7.3
                  Symbian OS v9.4 / Symbian^1, S60 5th Edition
                  on N97

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lycis
                    wrote on last edited by
                    #10

                    Wel, let me give a tested example coding :)

                    useful.h:
                    @
                    #ifndef USEFUL_H
                    #define USEFUL_H

                    namespace useful{
                    double power(double d);
                    }

                    #endif // USEFUL_H

                    @

                    useful.cpp:
                    @
                    #include "useful.h"

                    double useful::power(double d)
                    {
                    return (d*d);
                    }
                    @

                    main.cpp:
                    @
                    #include "useful.h"
                    #include <iostream>

                    using namespace useful;

                    int main(int argc, char *argv[])
                    {
                    std::cout << "power: " << power(2.0) << std::endl;
                    return 0;
                    }
                    @

                    This code uses the namespace useful that is defined in useful.h to call the function useful::power(double d) and prints the result to the default output.

                    Hope this helps you somewhat more.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pascal95abc
                      wrote on last edited by
                      #11

                      Thank you, this is great.
                      It was exactly what I was looking for. Now I will extend this code with really useful functions and some structs I want to access to from other projects easily ;)

                      Qt 4.7.3
                      Symbian OS v9.4 / Symbian^1, S60 5th Edition
                      on N97

                      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