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. Fake Expected a declaration
Forum Updated to NodeBB v4.3 + New Features

Fake Expected a declaration

Scheduled Pinned Locked Moved General and Desktop
expected a declqtcreator
8 Posts 4 Posters 4.0k Views 2 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.
  • J Offline
    J Offline
    Jan Sheva
    wrote on last edited by Jan Sheva
    #1

    Hello. There is some test to make a function that return a pointer to matrix array
    Program bullding is succesful and it work succesful. A result in QTextEdit looks like this:
    1,2,3,4,5,6,7,8,
    9,10,11,12,13,14,15,16,
    17,18,19,20,21,22,23,24,
    25,26,27,28,29,30,31,32,

    But QT Creator say "Expected a declaration" (see screen http://imageshack.com/a/img673/9086/WodPUb.jpg)
    My questions:

    1. What a reason of that QT Creator say " Expected a declaration" and can not find the declaration in this function: BYTE (*MyProg::GetProgressiveMatrix(int n))[8] ?
    2. Is there some other way to return a pointer to matrix array BYTE WPlanMap[4][8];

    Code:
    //----------------------------------------------Header.h
    #ifndef Test_H
    #define Test_H
    #include "ui_USEROK.h"
    #include <QtGui>
    #include <QtCore>
    class MyProg : public QWidget, public Ui::MyProg
    {
    Q_OBJECT
    public:
    MyProg(QWidget* pwgt = 0, Qt::WindowFlags f = 0);
    BYTE (*GetProgressiveMatrix(int n))[8];
    public slots:
    void Deal3Start();
    protected:
    BYTE WPlanMap[4][8];
    };
    #endif

    //---------------------------------Source.h

    #include "Header.h"
    #include <cmath>
    #include <cstdlib>
    #include <cstdio>
    MyProg::MyProg(QWidget* pwgt,Qt::WindowFlags f) : QWidget(pwgt,f){
    setupUi(this);
    setFixedSize(700, 600);
    connect(Deal3Button, SIGNAL(clicked()), this, SLOT(Deal3Start()));
    }
    void MyProg::Deal3Start(){
    QString ResultStr = "";
    QString TempStr;
    BYTE (*pWPlanMap)[8];
    pWPlanMap = GetProgressiveMatrix(TestBox->value());

    for (int i = 0; i < 4; ++i){
        for (int j = 0; j < 8; ++j){
            ResultStr = ResultStr + TempStr.setNum(pWPlanMap[i][j]) + ",";
        }
        ResultStr = ResultStr + '\n';
    }
    TestResult->setText(ResultStr); //Result string
    

    }
    BYTE (MyProg::GetProgressiveMatrix(int n))[8] //Expected a declaration ???(see screen http://imageshack.com/a/img673/9086/WodPUb.jpg)
    {
    for (int i = 0; i < 4; ++i){
    for (int j = 0; j < 8; ++j){
    WPlanMap[i][j] = n+j+(i
    8);
    }
    }
    return WPlanMap;
    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      BYTE (*GetProgressiveMatrix(int n))[8]; is not a declaration. It's a something of a mix between a function and an array declaration.

      You can return a BYTE ** from your function. Depending on what you actually need that matrix for, Qt provides QGenericMatrix that you can use.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jan Sheva
        wrote on last edited by
        #3

        Thanks. It is interesting.
        For example I have matrix
        int Map[2][4] = {......};

        How to create a pointer to Map and return it. Can you code declaration of such function and create a pointer to Map that returned?
        and then print Map in loop

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          returning multi-dim arrays from functions has ugly syntax.
          please see
          http://stackoverflow.com/questions/3716595/c-returning-multidimension-array-from-function

          something like

          #include <QDebug>
          
          const int MaxDim = 5;
          typedef int ( *myarray_return ) [MaxDim];
          myarray_return get_array()
          {
              static int local_myarray[MaxDim][MaxDim];
              for ( int i = 0; i < MaxDim; i ++ )
                  for ( int h = 0; h < MaxDim; h++ )
                  { local_myarray[i][h] = i * 10; }
          
              return local_myarray;
          
          }
          
          int main ( int argc, char* argv[] )
          {
             myarray_return ref_array = get_array();
              for ( int i = 0; i < MaxDim; i ++ )
                  for ( int h = 0; h < MaxDim; h++ )
                  { qDebug() << ref_array[i][h]; }
          
          }
          

          ps. sorry for no code formatting. Seems not be included in the editor anymore.

          [edit: Added missing coding tags: ``` before and after the code SGaist]

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Huulivoide
            wrote on last edited by
            #5

            int **matrix;
            is a 2 dimensional matrix, pointer of pointers, or dynamically allocated array
            of pointers (dynamically allocated arrays).

            Init like:

            int width 5;
            int height = 5;
            
            int **matrix = new int*[height];
            for (int row = 0; row < height; ++row)
            {
                matrix[row] = new int[width];
            }
            
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              There's a summary of the markdown tags used by the forum on the bottom of the page

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              mrjjM 1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • SGaistS SGaist

                  There's a summary of the markdown tags used by the forum on the bottom of the page

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @SGaist ahh thanks. I keep forgetting. When editor open, its not visible unless you scroll :)

                  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