[Solved] Using C++ class in projects



  • Hello,

    I created a c++ class which I want to use in my projects.
    Then I added the path to my pro file with INCLUDEPATH += "../_useful/" and now I want to use those functions.
    In my directory specified with includepath, there is the cpp, the header and a file with no extension which contains the code to include the header file so that I can include the class more easily.
    Now, I tried
    @#include <Useful>@ but I want namespace too!
    So how to do so ??
    @using namespace Useful;@ didnt work with the errors:
    \qtsdk\projects\projekte\eigene\SyncIt\mainwindow.cpp:14: error:'Useful' is not a namespace-name

    Thank you for helping me


  • Moderators

    Are you wrapping your class definition properly with the namespace?

    .h:
    @
    namespace Useful {

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

    }
    @

    .cpp:
    @
    namespace Useful {

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

    }
    @



  • 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.



  • 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.



  • 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?



  • 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);
    @



  • 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);@ ?



  • 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 :)



  • 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.



  • 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.



  • 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 ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.