Results 1 to 2 of 2

Thread: undefined reference to ..issue involving the QVector class

  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default undefined reference to ..issue involving the QVector class

    Hi All,
    I am a newbie and I need help with this (urgently if possible).
    I am deriving a template class from QVector in order to add more functionality to be able to convert the types store in the QVector and also carry out math operations like multiplications.



    Qt Code:
    1. #ifndef MVECTOR_H
    2. #define MVECTOR_H
    3. #include <QVector>
    4. #include <typeinfo>
    5. #include <QString>
    6.  
    7. template <typename T>
    8. class mVector:public QVector<T>
    9. {
    10.  
    11. bool numeric;
    12. public:
    13. mVector<T>():QVector<T>(){numeric=false;} //the vector contains non-numeric by default.
    14. mVector<T> operator*(mVector<T> a);
    15. bool isNumeric();
    16. mVector<float> toNumeric();
    17. mVector<QString> toString();
    18. mVector<T> operator=(mVector<T>);
    19. };
    20. #endif //
    21.  
    22.  
    23. #include "mvector.h"
    24.  
    25. template <typename T>
    26. mVector<T> mVector<T>::operator*(mVector<T> a){
    27. mVector<T> c;
    28. if(this->size()==a.size())
    29. {
    30. for(int i=0;i<a.size();i++)
    31. c[i]=this->at(i)*a[i];
    32. }
    33. return c;
    34. }
    35. template <typename T>
    36. bool mVector<T>::isNumeric(){
    37. return numeric;
    38. }
    39.  
    40. template <typename T>
    41. mVector<float> mVector<T>::toNumeric(){
    42. mVector<float> tm;
    43. if(!numeric){
    44. for(int i=0;i<this->size();i++)
    45. tm.append(float(this->at(i)));
    46. //this=&tm;
    47. }
    48. tm.numeric=true;
    49. return tm;
    50. }
    51.  
    52. template <typename T>
    53. mVector<QString> mVector<T>::toString(){
    54. mVector<QString> strv;
    55. if(numeric){
    56. for(int i=0;i<this->size();i++)
    57. {
    58. strv.append(QString::number(this->at(i)));
    59. }
    60. }
    61. strv.numeric=false;
    62. return strv;
    63. }
    64. template <typename T>
    65. mVector<T> mVector<T>::operator=(mVector<T> a){
    66. this->resize(a.size());
    67. for(int i=0;i<this->size();i++)
    68. *this[i]=a[i];
    69. return *this;
    70. }
    To copy to clipboard, switch view to plain text mode 


    creating an instance and calling any of its functions

    Qt Code:
    1. mVector<QString> s;
    2. mVector<float> n;
    3. s.append("3.445");
    4. n=s.toNumeric();
    To copy to clipboard, switch view to plain text mode 


    gives this error. i included all dependencies to no avail.

    ./debug\mainwindow.o:L:\Qt Codes\LoadCombination\LoadCombination-build-desktop/../LoadCombination/mainwindow.cpp:156: undefined reference to `mVector<float>::operator=(mVector<float>)'


    Thanks in advance for your intervention and help.

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: undefined reference to ..issue involving the QVector class

    I think you need to put the function definitions in the header file as well (instead of putting them in a separate .cpp-file, that is), after the class declaration; like:

    Qt Code:
    1. // mvector.h
    2. template <typename T>
    3. class mVector:public QVector<T>
    4. {
    5. // ...
    6. };
    7.  
    8. template <typename T>
    9. mVector<T> mVector<T>::operator*(mVector<T> a){
    10. // ... rest of implementation
    To copy to clipboard, switch view to plain text mode 

    This problem is discussed in the C++ FAQ:
    http://www.parashift.com/c++-faq-lit...html#faq-35.12

Similar Threads

  1. undefined reference
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2009, 13:45
  2. Undefined reference to crt
    By derektaprell in forum Installation and Deployment
    Replies: 0
    Last Post: 20th October 2009, 08:34
  3. Undefined Reference To...
    By ManuMies in forum Qt Programming
    Replies: 6
    Last Post: 10th February 2009, 12:14
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. Undefined reference
    By Salazaar in forum Newbie
    Replies: 12
    Last Post: 23rd May 2007, 10:21

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.