Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Problems with prototype
Forum Updated to NodeBB v4.3 + New Features

Problems with prototype

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 4 Posters 3.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.
  • S Offline
    S Offline
    spode
    wrote on last edited by
    #1

    @
    #ifndef IMPOSTAZIONI_H
    #define IMPOSTAZIONI_H

    #include <QSize>

    class Impostazioni
    {
    public:
    Impostazioni();
    static QSize getScreenSize();
    static void setScreenSize(int newScreenWidth, int newScreenHeight);

    private:
    int screenWidth;
    int screenHeight;
    };

    #endif // IMPOSTAZIONI_H
    @

    @
    #include "impostazioni.h"

    Impostazioni::Impostazioni()
    {

    }

    static QSize Impostazioni::getScreenSize()
    {
    if(screenWidth != 0 || screenHeight != 0)
    return QSize(screenWidth, screenHeight);
    else
    return QSize(320, 640);
    }

    static void Impostazioni::setScreenSize(int newScreenWidth, int newScreenHeight)
    {
    screenWidth = newScreenWidth;
    screenHeight = newScreenHeight;
    }

    @

    the errors are:
    @
    ..\TemperamentvolleSMS\impostazioni.cpp:8: error: cannot declare member function 'static QSize Impostazioni::getScreenSize()' to have static linkage
    ..\TemperamentvolleSMS/impostazioni.h: In static member function 'static QSize Impostazioni::getScreenSize()':
    ..\TemperamentvolleSMS/impostazioni.h:14: error: invalid use of member 'Impostazioni::screenWidth' in static member function
    ..\TemperamentvolleSMS\impostazioni.cpp:10: error: from this location
    ..\TemperamentvolleSMS/impostazioni.h:15: error: invalid use of member 'Impostazioni::screenHeight' in static member function
    ..\TemperamentvolleSMS\impostazioni.cpp:10: error: from this location
    ..\TemperamentvolleSMS/impostazioni.h:14: error: invalid use of member 'Impostazioni::screenWidth' in static member function
    ..\TemperamentvolleSMS\impostazioni.cpp:11: error: from this location
    ..\TemperamentvolleSMS/impostazioni.h:15: error: invalid use of member 'Impostazioni::screenHeight' in static member function
    ..\TemperamentvolleSMS\impostazioni.cpp:11: error: from this location
    ..\TemperamentvolleSMS\impostazioni.cpp: At global scope:
    ..\TemperamentvolleSMS\impostazioni.cpp:16: error: cannot declare member function 'static void Impostazioni::setScreenSize(int, int)' to have static linkage
    ..\TemperamentvolleSMS/impostazioni.h: In static member function 'static void Impostazioni::setScreenSize(int, int)':
    ..\TemperamentvolleSMS/impostazioni.h:14: error: invalid use of member 'Impostazioni::screenWidth' in static member function
    ..\TemperamentvolleSMS\impostazioni.cpp:18: error: from this location
    ..\TemperamentvolleSMS/impostazioni.h:15: error: invalid use of member 'Impostazioni::screenHeight' in static member function
    ..\TemperamentvolleSMS\impostazioni.cpp:19: error: from this location
    mingw32-make[1]: *** [debug/impostazioni.o] Error 1
    mingw32-make: *** [debug] Error 2
    The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
    Error while building project TemperamentvolleSMS (target: Qt Simulator)
    When executing build step 'Make'
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sidewinder
      wrote on last edited by
      #2

      And your question is?
      Compiler errors are clear: you can't access non static class member variables from a static functions. Create an object of your class inside static function or declare member variables as static as well.

      "Never memorize what you can look up in books."
      Albert Einstein

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chuck Gao
        wrote on last edited by
        #3

        Error when define the function.

        1. "static" is not necessary
        2. The variant should be Impostazioni::screenWidth, Impostazioni::screenHeight since they're static
          @
          QSize Impostazioni::getScreenSize()
          {
          if(Impostazioni::screenWidth != 0 || Impostazioni::screenHeight != 0)
          return QSize(Impostazioni::screenWidth, Impostazioni::screenHeight);
          else
          return QSize(320, 640);
          }

        void Impostazioni::setScreenSize(int newScreenWidth, int newScreenHeight)
        {
        Impostazioni::screenWidth = newScreenWidth;
        Impostazioni::screenHeight = newScreenHeight;
        }
        @

        Try again :D

        Chuck

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chuck Gao
          wrote on last edited by
          #4

          Also, make screenWidth and screenHeight as static member

          Chuck

          1 Reply Last reply
          0
          • S Offline
            S Offline
            spode
            wrote on last edited by
            #5

            so, i tried:
            @
            #ifndef IMPOSTAZIONI_H
            #define IMPOSTAZIONI_H

            #include <QSize>

            class Impostazioni
            {
            public:
            Impostazioni();
            static QSize getScreenSize();
            static void setScreenSize(int newScreenWidth, int newScreenHeight);

            private:
            static int screenWidth;
            static int screenHeight;
            };

            #endif // IMPOSTAZIONI_H
            @

            @
            #include "impostazioni.h"

            Impostazioni::Impostazioni()
            {

            }

            static QSize Impostazioni::getScreenSize()
            {
            if(Impostazioni::screenWidth != 0 || Impostazioni::screenHeight != 0)
            return QSize(Impostazioni::screenWidth, Impostazioni::screenHeight);
            else
            return QSize(320, 640);
            }

            static void Impostazioni::setScreenSize(int newScreenWidth, int newScreenHeight)
            {
            Impostazioni::screenWidth = newScreenWidth;
            Impostazioni::screenHeight = newScreenHeight;
            }
            @

            @
            ..\TemperamentvolleSMS\impostazioni.cpp:8: error: cannot declare member function 'static QSize Impostazioni::getScreenSize()' to have static linkage
            ..\TemperamentvolleSMS\impostazioni.cpp:16: error: cannot declare member function 'static void Impostazioni::setScreenSize(int, int)' to have static linkage
            mingw32-make[1]: Leaving directory `C:/Users/7-Spode/Documents/Progetti/QT/mobile/TemperamentvolleSMS-build-simulator'
            mingw32-make[1]: *** [debug/impostazioni.o] Error 1
            mingw32-make: *** [debug] Error 2
            The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
            Error while building project TemperamentvolleSMS (target: Qt Simulator)
            When executing build step 'Make'
            @

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

              Remove static keyword from implementation code. Leave it only in header definitions

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chuck Gao
                wrote on last edited by
                #7

                As mentioned before, remove "static" in implementation.
                You need init your static data member in .cpp file if it's not const

                @
                // cpp file
                int Impostazioni::screenWidth = 0 // like this, in out of function scope

                void Impostazioni::setScreenSize(int newScreenWidth, int newScreenHeight)
                {
                Impostazioni::screenWidth = newScreenWidth; // When using inside class, you can just use screenWidth
                Impostazioni::screenHeight = newScreenHeight; // But outside class, you should use Impostazioni::screenHeight if you want to access it(should be public)
                }

                @

                Chuck

                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