sorting of classes not working

Hi all.

I have a class "ExampleSentences" with a private member
std::string _simpHanzi;
I have another class "Vocabulary" with a private member
std::vector<ExampleSentences> _exampleSentence
I want to sort those sentences via the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Vocabulary::sentenceIsLonger(const ExampleSentences &firstSentence,const  ExampleSentences &secondSentence) {
    int firstSentenceLength = firstSentence.getSimpHanzi().length();
    int secondSentenceLength = secondSentence.getSimpHanzi().length();
    if (firstSentenceLength > secondSentenceLength) {
        return true;
    } else {
        return false;
    }
}

void Vocabulary::sortSentences() {
    std::sort(_exampleSentence.begin(),_exampleSentence.end(), sentenceIsLonger);
}

I get the following error message:
Vocabulary.cpp: In member function 'bool Vocabulary::sentenceIsLonger(const ExampleSentences&, const ExampleSentences&)':
Vocabulary.cpp:68: error: passing 'const ExampleSentences' as 'this' argument of 'std::string ExampleSentences::getSimpHanzi()' discards qualifiers
Vocabulary.cpp:69: error: passing 'const ExampleSentences' as 'this' argument of 'std::string ExampleSentences::getSimpHanzi()' discards qualifiers
Vocabulary.cpp: In member function 'void Vocabulary::sortSentences()':
Vocabulary.cpp:78: error: argument of type 'bool (Vocabulary::)(const ExampleSentences&, const ExampleSentences&)' does not match 'bool (Vocabulary::*)(const ExampleSentences&, const ExampleSentences&)'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = ExampleSentences, _Compare = bool (Vocabulary::*)(const ExampleSentences&, const ExampleSentences&)]':
In file included from c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/algorithm:62,
                 from Vocabulary.cpp:12:
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:2301:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ExampleSentences*, std::vector<ExampleSentences, std::allocator<ExampleSentences> > >, _Size = int, _Compare = bool (Vocabulary::*)(const ExampleSentences&, const ExampleSentences&)]'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:5258:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ExampleSentences*, std::vector<ExampleSentences, std::allocator<ExampleSentences> > >, _Compare = bool (Vocabulary::*)(const ExampleSentences&, const ExampleSentences&)]'
Vocabulary.cpp:78:   instantiated from here
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:124: error: must use '.*' or '->*' to call pointer-to-member function in '__comp (...)', e.g. '(... ->* __comp) (...)'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:125: error: must use '.*' or '->*' to call pointer-to-member function in '__comp (...)', e.g. '(... ->* __comp) (...)'

and a lot more similar stuff

Any ideas?
closed account (o3hC5Di1)
Hi there,

Not exactly sure, but:

Vocabulary.cpp: In member function 'bool Vocabulary::sentenceIsLonger(const ExampleSentences&, const ExampleSentences&)':
Vocabulary.cpp:68: error: passing 'const ExampleSentences' as 'this' argument of 'std::string ExampleSentences::getSimpHanzi()' discards qualifiers
Vocabulary.cpp:69: error: passing 'const ExampleSentences' as 'this' argument of 'std::string ExampleSentences::getSimpHanzi()' discards qualifiers


Try not passing those ExampleSentence'ss as const, it seems to me like getSimpHanzi is doing something to the object that does not go with it being constant.

Hope that helps.

All the best,
NwN
Thanks for looking into it.
The
getSimpHanzi
function is as simple as can be:
1
2
3
std::string ExampleSentences::getSimpHanzi() {
    return _simpHanzi;
}

Not passing as const does not change the error message:
Vocabulary.cpp: In member function 'void Vocabulary::sortSentences()':
Vocabulary.cpp:78: error: argument of type 'bool (Vocabulary::)(ExampleSentences&, ExampleSentences&)' does not match 'bool (Vocabulary::*)(ExampleSentences&, ExampleSentences&)'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = ExampleSentences, _Compare = bool (Vocabulary::*)(ExampleSentences&, ExampleSentences&)]':
and so on.

Maybe it has something to with
(Vocabulary::)(ExampleSentences&, ExampleSentences&)' does not match 'bool (Vocabulary::*)

Specifically
(Vocabulary::) vs  (Vocabulary::*) 

Any other ideas?
Vocabulary::sentenceIsLonger() should be a static member function. After that I think it should work as it is.
Declare the function inside the class as static

static bool sentenceIsLonger(const ExampleSentences &firstSentence,const ExampleSentences );
Thanks for the input, I am a noob, so I don't have experience with static member functions. I just put static before the declaration and the implementation:

1
2
3
4
5
6
7
8
9
static bool Vocabulary::sentenceIsLonger( ExampleSentences &firstSentence,  ExampleSentences &secondSentence) {
    int firstSentenceLength = firstSentence.getSimpHanzi().length();
    int secondSentenceLength = secondSentence.getSimpHanzi().length();
    if (firstSentenceLength < secondSentenceLength) {
        return true;
    } else {
        return false;
    }
}


This resulted in a different error message:
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/mkspecs/default' -o debug/Vocabulary.o Vocabulary.cpp
Vocabulary.cpp:67: error: cannot declare member function 'static bool Vocabulary::sentenceIsLonger(ExampleSentences&, ExampleSentences&)' to have static linkage
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = ExampleSentences, _Compare = bool (*)(ExampleSentences&, ExampleSentences&)]':
In file included from c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/algorithm:62,
                 from Vocabulary.cpp:12:
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:2301:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ExampleSentences*, std::vector<ExampleSentences, std::allocator<ExampleSentences> > >, _Size = int, _Compare = bool (*)(ExampleSentences&, ExampleSentences&)]'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:5258:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ExampleSentences*, std::vector<ExampleSentences, std::allocator<ExampleSentences> > >, _Compare = bool (*)(ExampleSentences&, ExampleSentences&)]'
Vocabulary.cpp:78:   instantiated from here
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:124: error: invalid initialization of reference of type 'ExampleSentences&' from expression of type 'const ExampleSentences'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:125: error: invalid initialization of reference of type 'ExampleSentences&' from expression of type 'const ExampleSentences'
Ok, I just read, that "static" only goes in the header file. However like that it still results in:
++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.1/mingw/mkspecs/default' -o debug/Vocabulary.o Vocabulary.cpp
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = ExampleSentences, _Compare = bool (*)(ExampleSentences&, ExampleSentences&)]':
In file included from c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/algorithm:62,
                 from Vocabulary.cpp:12:
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:2301:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ExampleSentences*, std::vector<ExampleSentences, std::allocator<ExampleSentences> > >, _Size = int, _Compare = bool (*)(ExampleSentences&, ExampleSentences&)]'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:5258:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ExampleSentences*, std::vector<ExampleSentences, std::allocator<ExampleSentences> > >, _Compare = bool (*)(ExampleSentences&, ExampleSentences&)]'
Vocabulary.cpp:78:   instantiated from here
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:124: error: invalid initialization of reference of type 'ExampleSentences&' from expression of type 'const ExampleSentences'
c:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:125: error: invalid initialization of reference of type 'ExampleSentences&' from expression of type 'const ExampleSentences'
You shall not specify storage-class specifier static for the function definition. It should be present only in the function declaration inside the class definition.

According to paragraph #5 of section 9.4 Static members of the C++ Standard

When used in the declaration of a class member, the static specifier shall only be used in the member declarations that appear within the member-specification of the class definition. [ Note: It cannot be specified in member declarations that appear in namespace scope. —end note ]
Last edited on
SOLVED: I had to make .getSimpHanzi a const function as in:
std::string getSimpHanzi() const;

Damn... I still get an error message:
1
2
3
4
5
6
7
Vocabulary.h: In static member function 'static bool Vocabulary::sentenceIsLonger(const ExampleSentences&, const ExampleSentences&)':
In file included from MainWnd.h:17,
                 from main.cpp:3:
Vocabulary.h:37: error: passing 'const ExampleSentences' as 'this' argument of 'std::string ExampleSentences::getSimpHanzi()' discards qualifiers
Vocabulary.h:38: error: passing 'const ExampleSentences' as 'this' argument of 'std::string ExampleSentences::getSimpHanzi()' discards qualifiers
make[1]: *** [debug/main.o] Error 1
make: *** [debug] Error 2


The lines 37 and 38 are here:
1
2
3
4
5
6
7
8
9
    static bool sentenceIsLonger(const ExampleSentences &firstSentence,const ExampleSentences &secondSentence) {
        int firstSentenceLength = firstSentence.getSimpHanzi().length();
        int secondSentenceLength = secondSentence.getSimpHanzi().length();
        if (firstSentenceLength < secondSentenceLength) {
            return true;
        } else {
            return false;
        }
    };


Wlad, I am not sure if my modification is what you had in mind.
Last edited on
Did you added const to getSimpHanzi() in the declaration and definition as well?
BTW if sentenceIsLonger() is no longer a member function, then you don't need static.

Also the best would to compress your problem into as few lines as possible then post the *whole* thing here. We have to guess the code you don't send from the error messages.
Last edited on
Topic archived. No new replies allowed.