Reputation: 175
I have this .h file
#ifndef VISUALSETTINGS_H
#define VISUALSETTINGS_H
#include <QPair>
#include <QDataStream>
class VisualSettings
{
public:
VisualSettings();
VisualSettings(QPair<int, int>, QPair<int, int>);
QPair<int, int> getUpper();
void setUpper(QPair<int, int>);
QPair<int, int> getLower();
QPair<int, int> setLower(QPair<int, int>);
private:
QPair<int, int> upper;
QPair<int, int> lower;
};
QDataStream &operator<<(QDataStream &out, const VisualSettings &vs);
QDataStream &operator>>(QDataStream &in, VisualSettings &vs);
#endif // VISUALSETTINGS_H
And i have this .cpp(a part of it)
#include "visualsettings.h"
VisualSettings::VisualSettings()
{
upper.first = 0;
upper.second = 0;
lower.first = 0;
lower.second = 0;
}
VisualSettings::VisualSettings(QPair<int, int> u, QPair<int, int> l)
{
upper.first = u.first;
upper.second = u.second;
lower.first = l.first;
lower.second = l.second;
}
QPair<int, int> VisualSettings::getUpper()
{
return upper;
}
I am testing this class like this:
QPair<int, int> q1(2,3);
QPair<int, int> q2(4, 5);
VisualSettings v(q1, q2);
//QPair<int, int> q3 = v.getUpper();
//cout << v.getUpper().first;// << " - " << vs.getUpper().second << endl;
I get this error for this constructor:
main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall VisualSettings::VisualSettings(struct QPair,struct QPair)" (??0VisualSettings@@QAE@U?$QPair@HH@@0@Z) referenced in function _main
If i use VisualSettings vs() when i do vs.getUpper() i get the error:
C:\Users\Alex\testing-build-desktop-Qt_4_8_0_for_Desktop_-MSVC2010_Qt_SDK__Debug..\testing\main.cpp:23: error: C2228: left of '.getUpper' must have class/struct/union
I noticed that even if a declare a QList and use list.at(0) i get the same error(must have...).
What could be the problem?
LATER EDIT The answer from @johnathon helped me, but I have another problem. I overwrite the operators << and >> like this:
QDataStream &operator<<(QDataStream &out, const VisualSettings &vs)
{
out << vs.getUpper().first << vs.getUpper().second;
out << vs.getLower().first << vs.getLower().second;
return out;
}
QDataStream &operator>>(QDataStream &in, const VisualSettings &vs)
{
QPair uaux(0, 0);
in >> uaux.first >> uaux.second;
vs.setUpper(uaux);
QPair laux(0, 0);
in >> laux.first >> laux.second;
vs.setLower(laux);
return in;
}
I get this error:
C:\Users\Alex\testing-build-desktop-Qt_4_8_0_for_Desktop_-MSVC2010_Qt_SDK__Debug..\testing\visualsettings.cpp:42: error: C2662: 'VisualSettings::getUpper' : cannot convert 'this' pointer from 'const VisualSettings' to 'VisualSettings &' Conversion loses qualifiers
C:\Users\Alex\testing-build-desktop-Qt_4_8_0_for_Desktop_-MSVC2010_Qt_SDK__Debug..\testing\visualsettings.cpp:42: error: C2228: left of '.first' must have class/struct/union
What could be the problem?
Also I have another question. If at another class i use an VisualSettings object, in .h i declare it like VisualSettings vs, in the constructor of that class, can i say vs = new VisualSettings(1,2) or vs(1,2); so the question is how i call the constructor of VisualSetting in the constructor of another class(if the VisualSettigs object is variable of the class)?
LATER LATER EDIT:
I still got errors at my program and I don't know how to solve it. So I wanted to overwite operator >>
class VisualSettings
{
............
};
QDataStream &operator<<(QDataStream &out, const VisualSettings &vs);
QDataStream &operator>>(QDataStream &in, VisualSettings &vs);
In the .cpp
QDataStream &operator<<(QDataStream &out, const VisualSettings &vs)
{
out << vs.getUpper().first << vs.getUpper().second;
out << vs.getLower().first << vs.getLower().second;
return out;
}
I get this error:
C:\Users\Alex\testing-build-desktop-Qt_4_8_0_for_Desktop_-MSVC2010_Qt_SDK__Release..\testing\visualsettings.cpp:44: error: C2662: 'VisualSettings::getLower' : cannot convert 'this' pointer from 'const VisualSettings' to 'VisualSettings &' Conversion loses qualifiers C:\Users\Alex\testing-build-desktop-Qt_4_8_0_for_Desktop_-MSVC2010_Qt_SDK__Release..\testing\visualsettings.cpp:44: error: C2228: left of '.first' must have class/struct/union
I have another class
class Playlist
{
................
};
bool caseArtistLessThan(const SongInfo &s1, const SongInfo &s2);
In .cpp i have:
bool caseArtistLessThan(const SongInfo &s1, const SongInfo &s2)
{
QString str1(ID3_GetArtist(&s1.getTag()));
QString str2(ID3_GetArtist(&s2.getTag()));
return str1 < str2;
}
I get the error:
C:\Users\Alex\testing-build-desktop-Qt_4_8_0_for_Desktop_-MSVC2010_Qt_SDK__Release..\testing\playlist.cpp:20: error: C2662: 'SongInfo::getTag' : cannot convert 'this' pointer from 'const SongInfo' to 'SongInfo &' Conversion loses qualifiers C:\Users\Alex\testing-build-desktop-Qt_4_8_0_for_Desktop_-MSVC2010_Qt_SDK__Release..\testing\playlist.cpp:20: error: C2662: 'SongInfo::getTag' : cannot convert 'this' pointer from 'const SongInfo' to 'SongInfo &' Conversion loses qualifiers
Can somebody help me solve this problems? I don't understand why it doesn't works. I've seen a lot of examples and I've done just like in that examples. What could be the problem?
Upvotes: 0
Views: 472
Reputation: 13130
About second error. You cannot pass const parameter to operator >> as you will be modifying it. const means no modification allowed to object.
Some other notes:
QPair has assign operator so you don't need to call
upper.first = u.first;
upper.second = u.second;
instead use
upper = u;
There is also such thing as initialization list and you may assign it even like this:
VisualSettings::VisualSettings(QPair<int, int> u, QPair<int, int> l)
: upper(u)
, lower(l)
{
}
Upvotes: 1