0

I have this problem with sending array from myclass object to myclass2 function named receive(). The name of array is probesTab1 in myclass.

I try to use connect method to do this.

I have this error: undefined reference to myclass2::receive(int*)

I'm sure it's some basic knowledge, but I cant't move for few days.

"myclass.h"

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>

class myclass : public QObject
{
   Q_OBJECT

public:

   explicit myclass(QObject *parent = nullptr);

   int probesTab1[3];

signals:

   void pass(int * tab);

public slots:
};

#endif // MYCLASS_

myclass2.h

#ifndef MYCLASS2_H
#define MYCLASS2_H
#include <QObject>

class myclass2 : public QObject
{
    Q_OBJECT

public:

    myclass2();
    int array[3];

public slots:

    void receive(int *tab);
};

#endif // MYCLASS2_H

Main.

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include "myclass.h"
#include "myclass2.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

myclass Class1;
myclass2 Class2;

for(int i = 0; i<3 ; i++)
{
Class1.probesTab1[i] = i;      
//I need this array to go into myclass2 function named receive()
}

int *ptr;
ptr = Class1.probesTab1;

QObject::connect(&Class1,SIGNAL(pass(int)),&Class2,SLOT(receive(int)));

emit Class1.pass(ptr);


return a.exec();
}

myclass2.cpp

#include "myclass2.h"
#include <QDebug>

myclass2::myclass2()
{
}

void receive(int values[])
{
   for(int i = 0; i<3; i++)
{
    qDebug() << values[i];
}
}
Bartek
  • 49
  • 6
  • QObject::connect(&Class1,SIGNAL(pass(int)),&Class2,SLOT(receive(int))); The recipient and sender should not be a pinter? SIGNAL(pass(int*)), &Class2, SLOT(receive(int*)) – Tazo leladze Sep 21 '17 at 10:49
  • 1
    In `myclass2.cpp` receive function definition defines just a free function, not a `myclass2` member. You should change it to `void myclass2::receive(int * values)` – user7860670 Sep 21 '17 at 10:51
  • 1
    You can use QVector instead of sending array – saeed Sep 21 '17 at 10:53
  • 1
    Is it actually requirement to use signals/slots? Otherwise you could simply do: `myclass2.receive(myclass.probesTab1);`. – vahancho Sep 21 '17 at 10:54
  • Why signal/slot for such request , is it a practice? – saeed Sep 21 '17 at 10:54
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – m7913d Sep 21 '17 at 13:10

1 Answers1

-3

It's better to do such operation in particular ways described here.

But as you are practicing read more about QObject::connect

just change your connect

 QObject::connect(&Class1,SIGNAL(pass(int*)),&Class2,SLOT(receive(int*)));

and receive function

void myclass2::receive(int *values)
{
    for(int i = 0; i<3; i++)
    {
        qDebug() << values[i];
    }
}
saeed
  • 2,477
  • 2
  • 23
  • 40