Problems with prototype



  • @
    #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'
    @



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



  • 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



  • Also, make screenWidth and screenHeight as static member



  • 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'
    @



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



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

    @


Log in to reply
 

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