PDA

View Full Version : Error passing struct by reference to a function in another class.



marcos.miranda
14th May 2018, 15:35
Hello everyone.
I'm not able to pass a struct as a reference to a function in another class in Qt.

I was able to do this on the site "https://ideone.com" as per the code below:

It worked



#include <iostream>
using namespace std;

struct stcTeste {
string vstrNome="Oiaaaa";
};

class clsRef{
public:
void fcDados (stcTeste &pstcTesteDados) {
stcTeste stcLocal;
stcLocal = pstcTesteDados;
cout << stcLocal.vstrNome << endl;
}
};

int main() {
clsRef *vobjRef = new clsRef();
stcTeste stcDados;
stcDados.vstrNome="Oi...";
vobjRef->fcDados(stcDados);

return 0;
}


Already in Qt the code below returns the following error:

Does not work !!!



//Arquivo : clsteste.h

class clsTeste : public QWidget
{
Q_OBJECT
public:
explicit clsTeste(QWidget *parent = nullptr);

struct stcInfoXml
{
QString vstrNome = "Oiaaaa";
};

void fcSalvarCadastroEmpresaDBXml(QString &pstrCadEmpresa, stcInfoXml &pstcInfoXmlDados);
};

//Arquivo : clsteste.cpp

#include "clsteste.h"
void clsTeste::fcSalvarCadastroEmpresaDBXml(QString &pstrCadEmpresa, stcInfoXml &pstcInfoXmlDados)
{
QString vstrLocal;
stcInfoXml vstcLocal;
vstrLocal = pstrCadEmpresa;
vstcLocal = pstcInfoXmlDados;
}

//Arquivo : frmMainWindow.h

#include <clsteste.h>
Public:
typedef struct stcInfo
{
QString vstrNome = "Oiaaaa";
}stcInfoDados;

Private:
void fcMontarDadosXml();

//Arquivo : frmMainWindow.cpp

#include "frmMainWindow.h"
QString vpTeste = "Oiaaa";
clsTeste *vobjTesteDBXml = new clsTeste();
vobjTesteDBXml->fcSalvarCadastroEmpresaDBXml(vpTeste, stcInfoDados);




usr/include/libdrm -I. -I../../5.10.1/gcc_64/mkspecs/linux-g++ -o frmMainWindow.o ../RWCXML/frmMainWindow.cpp
//../RWCXML/frmMainWindow.cpp: In member function ‘void clsMainWindow::fcMontarDadosXml()’:
//../RWCXML/frmMainWindow.cpp:390:75: error: expected primary-expression before ‘)’ token
// vobjTesteDBXml->fcSalvarCadastroEmpresaDBXml(vpTeste, stcInfoDados);
^
Makefile:1363: recipe for target 'frmMainWindow.o' failed
make: *** [frmMainWindow.o] Error 1
10:57:58: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project RWCXML (kit: Desktop Qt 5.10.1 GCC 64bit)
When executing step "Make"


Can anybody help me ?

Thanks in advance...

marcos.miranda
14th May 2018, 22:06
Hey guys.
Can someone help ?

:confused: :confused:

Thanks.

d_stranz
16th May 2018, 00:05
vobjTesteDBXml->fcSalvarCadastroEmpresaDBXml(vpTeste, stcInfoDados);

Learn to read and understand your compiler error messages. You seemed to pick just random lines of code to post as part of your question even though they have nothing to do with the error you report. It looks to me like the problem is that you declared a typedef of a struct, and named that typedef "stcInfoDados". You then try to pass that typedef name into a function. You haven't defined a variable named "stcInfoDados", you've simply said to the compiler, "Every time I use the name 'stcInfoDados', you should substitute the struct 'stcInfo'". In other words, you aren't passing a reference to a variable, you are passing the name of a type. It is no different than if you had written:


vobjTesteDBXml->fcSalvarCadastroEmpresaDBXml( vpTeste, double );

marcos.miranda
16th May 2018, 13:33
Hello, d_stranz.
Thank you for your attention.

After your explanation of the problem, I was able to guide and compile the code in question.
I researched more on the Internet and in Books to understand more about Typedef and Struct.

From what I analyzed, I had two problems:

The first was like you said, I defined a struct type but I did not instantiate a variable of this type and I passed a type to the function.

The second is that I thought that when we created two structs, with the same amount of elements and of the same types we could assign one to another.



struct stcInfo_1
{
QString vstrNome = "Inicio!!!";
};

struct stcInfo_2
{
QString vstrNome = "Inicio!!!";
};

struct stcInfo_1 vstcInfo_1;
struct stcInfo_2 vstcInfo_2;

vstcInfo_1 = vstcInfo_2; // Erro compiler...




But no, the compiled does not accept that.
They are different things "struct", even having the same look.

So again thank you very much for your guidance. :D

d_stranz
16th May 2018, 22:10
Two structs with different type names are different types in C++ even if their contents are identical. If you want to be able to assign a struct of one type to a struct of another type, then you need to provide at least an assignment operator and also usually a copy constructor:



struct stcInfo_1
{
QString vstrNome = "Inicio!!!";

stcInfo_1( const stcInfo_2 & rhs ) // Copy constructor ("rhs" = "right-hand side")
: vstrNone( rhs.vstrNone )
{}

const stcInfo_1 & operator=( const stcInfo_2 & rhs ) // Assignment operator
{
vstrNone = rhs.vstrNone;
return *this;
}

operator stcInfo_2 & () // Cast operator
{
return *this;
}

bool operator==( const stcInfo_2 & rhs ) const // Equality operator
{
return vstrNone == rhs.vstrNone;
}
};

struct stcInfo_2
{
QString vstrNome = "Inicio!!!";
};

Define the same thing for stcInfo_2 and you can create or assign an stcInfo_2 instance from an stcInfo_1 instance. Be very, very careful with the cast operator. In this case it will give correct results only because stcInfo_1 and stcInfo_2 are basically identical types. You should never create such an operator for structs or classes of different types.

marcos.miranda
17th May 2018, 12:42
Hi, d_stranz.

Thanks for the more helpful explanation, it will definitely help other people as well.

Thanks.