Results 1 to 6 of 6

Thread: Error passing struct by reference to a function in another class.

  1. #1
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Talking Error passing struct by reference to a function in another class.

    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

    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. struct stcTeste {
    5. string vstrNome="Oiaaaa";
    6. };
    7.  
    8. class clsRef{
    9. public:
    10. void fcDados (stcTeste &pstcTesteDados) {
    11. stcTeste stcLocal;
    12. stcLocal = pstcTesteDados;
    13. cout << stcLocal.vstrNome << endl;
    14. }
    15. };
    16.  
    17. int main() {
    18. clsRef *vobjRef = new clsRef();
    19. stcTeste stcDados;
    20. stcDados.vstrNome="Oi...";
    21. vobjRef->fcDados(stcDados);
    22.  
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Already in Qt the code below returns the following error:

    Does not work !!!

    Qt Code:
    1. //Arquivo : clsteste.h
    2.  
    3. class clsTeste : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit clsTeste(QWidget *parent = nullptr);
    8.  
    9. struct stcInfoXml
    10. {
    11. QString vstrNome = "Oiaaaa";
    12. };
    13.  
    14. void fcSalvarCadastroEmpresaDBXml(QString &pstrCadEmpresa, stcInfoXml &pstcInfoXmlDados);
    15. };
    16.  
    17. //Arquivo : clsteste.cpp
    18.  
    19. #include "clsteste.h"
    20. void clsTeste::fcSalvarCadastroEmpresaDBXml(QString &pstrCadEmpresa, stcInfoXml &pstcInfoXmlDados)
    21. {
    22. QString vstrLocal;
    23. stcInfoXml vstcLocal;
    24. vstrLocal = pstrCadEmpresa;
    25. vstcLocal = pstcInfoXmlDados;
    26. }
    27.  
    28. //Arquivo : frmMainWindow.h
    29.  
    30. #include <clsteste.h>
    31. Public:
    32. typedef struct stcInfo
    33. {
    34. QString vstrNome = "Oiaaaa";
    35. }stcInfoDados;
    36.  
    37. Private:
    38. void fcMontarDadosXml();
    39.  
    40. //Arquivo : frmMainWindow.cpp
    41.  
    42. #include "frmMainWindow.h"
    43. QString vpTeste = "Oiaaa";
    44. clsTeste *vobjTesteDBXml = new clsTeste();
    45. vobjTesteDBXml->fcSalvarCadastroEmpresaDBXml(vpTeste, stcInfoDados);
    To copy to clipboard, switch view to plain text mode 

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

    Can anybody help me ?

    Thanks in advance...

  2. #2
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Error passing struct by reference to a function in another class.

    Hey guys.
    Can someone help ?



    Thanks.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Error passing struct by reference to a function in another class.

    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 );
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    marcos.miranda (16th May 2018)

  5. #4
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Thumbs up Re: Error passing struct by reference to a function in another class.

    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.

    Qt Code:
    1. struct stcInfo_1
    2. {
    3. QString vstrNome = "Inicio!!!";
    4. };
    5.  
    6. struct stcInfo_2
    7. {
    8. QString vstrNome = "Inicio!!!";
    9. };
    10.  
    11. struct stcInfo_1 vstcInfo_1;
    12. struct stcInfo_2 vstcInfo_2;
    13.  
    14. vstcInfo_1 = vstcInfo_2; // Erro compiler...
    To copy to clipboard, switch view to plain text mode 


    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.

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Error passing struct by reference to a function in another class.

    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:

    Qt Code:
    1. struct stcInfo_1
    2. {
    3. QString vstrNome = "Inicio!!!";
    4.  
    5. stcInfo_1( const stcInfo_2 & rhs ) // Copy constructor ("rhs" = "right-hand side")
    6. : vstrNone( rhs.vstrNone )
    7. {}
    8.  
    9. const stcInfo_1 & operator=( const stcInfo_2 & rhs ) // Assignment operator
    10. {
    11. vstrNone = rhs.vstrNone;
    12. return *this;
    13. }
    14.  
    15. operator stcInfo_2 & () // Cast operator
    16. {
    17. return *this;
    18. }
    19.  
    20. bool operator==( const stcInfo_2 & rhs ) const // Equality operator
    21. {
    22. return vstrNone == rhs.vstrNone;
    23. }
    24. };
    25.  
    26. struct stcInfo_2
    27. {
    28. QString vstrNome = "Inicio!!!";
    29. };
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #6
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Thumbs up Re: Error passing struct by reference to a function in another class.

    Hi, d_stranz.

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

    Thanks.

Similar Threads

  1. Replies: 2
    Last Post: 20th January 2018, 23:32
  2. Replies: 3
    Last Post: 19th April 2016, 15:25
  3. Struct in a class
    By Atomic_Sheep in forum Newbie
    Replies: 6
    Last Post: 10th February 2012, 09:34
  4. Struct into a C++ class
    By franco.amato in forum General Programming
    Replies: 24
    Last Post: 30th September 2010, 16:13
  5. Qt::QueuedConnection and passing by Reference
    By soul_rebel in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2007, 18:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.