C ++(NetBeans)声明我正在使用一个在我的代码中没有出现的运算符?

时间:2014-05-08 00:23:28

标签: c++

我对使用NetBeans使用c ++获得的错误感到困惑。我不是最好的指针与非指针(本机Java编码器),所以请记住这一点。

我正在为uni中的数据结构制作一个“Netflix Challenge”的淡化版本,而我正在为每个用户显示前n部电影的最终方法。代码下面是我得到的(大量)错误消息。它似乎说我在MovieBase :: Movie上使用了一个小于运算符,但我只在浮点数上使用它 - 并且它声明它是从错误的行调用的(我在注释中标记了它)。

此外,最后的cout<<最后说的是放弃限定符的事情,在搜索之后我仍然不知道它试图告诉我什么。

任何人都可以提供一些见解吗?谢谢!

map<MovieBase::Movie,float> UserBase::topMovieList(int moviesToShow, MovieBase* mb, unsigned int userID){
map<MovieBase::Movie,float> topList;
vector<MovieBase::Movie>::iterator mit = mb->allMovies.begin();
while(mit!=mb->allMovies.end()){
    MovieBase::Movie thisMovie = *mit;
    vector<string> theseGenres = thisMovie.getGenres();
    float weightedScore = 0;
    for(int i=0;i<theseGenres.size();i++){
        weightedScore+=this->get(userID)->preferenceFactors.at(theseGenres[i])*(thisMovie.getAverage());
    }
    weightedScore = weightedScore/theseGenres.size();
    if(topList.empty()){
        map<MovieBase::Movie,float>::iterator bit = topList.begin();
        //The error occurs on the line below
        topList.insert(bit,std::make_pair(thisMovie,weightedScore));
    }
    else{
        map<MovieBase::Movie,float>::iterator bit = topList.begin();
        map<MovieBase::Movie,float>::iterator lit = topList.end();
        lit--;
        if(bit->second==lit->second||(weightedScore>bit->second)){
            while(bit->second<weightedScore){
                bit++;
            }
            topList.insert(bit,std::make_pair(thisMovie,weightedScore));
            if(topList.size()>moviesToShow){
                bit = topList.begin();
                topList.erase(bit);
            }
        }
    }
    mit++;
}
map<MovieBase::Movie,float>::iterator finalPrintIterator = topList.begin();
cout << "User " << userID << "'s Top " << moviesToShow << " movies:" << endl;
while(finalPrintIterator!=topList.end()){
    cout << finalPrintIterator->first.getName() << "(" << finalPrintIterator->first.getYear() << ") - Weighted Score: " << finalPrintIterator->second << endl;
    finalPrintIterator++;
}
return topList;
}







"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/s/Production/Projects/CSE250/250-REGAN-PROJ2/NetflixChallenge'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/netflixchallenge.exe
make[2]: Entering directory '/cygdrive/s/Production/Projects/CSE250/250-REGAN-PROJ2/NetflixChallenge'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f "build/Debug/Cygwin_4.x-Windows/UserBase.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/UserBase.o.d" -o build/Debug/Cygwin_4.x-Windows/UserBase.o UserBase.cpp
UserBase.cpp: In member function ‘std::map<MovieBase::Movie, float> UserBase::topMovieList(int, MovieBase*, unsigned int)’:
UserBase.cpp:146:51: error: passing ‘const MovieBase::Movie’ as ‘this’ argument of ‘std::string MovieBase::Movie::getName()’ discards qualifiers [-fpermissive]
         cout << finalPrintIterator->first.getName() << "(" << finalPrintIterator->first.getYear() << ") - Weighted Score: " << finalPrintIterator->second << endl;
                                                   ^
UserBase.cpp:146:97: error: passing ‘const MovieBase::Movie’ as ‘this’ argument of ‘unsigned int MovieBase::Movie::getYear()’ discards qualifiers [-fpermissive]
         cout << finalPrintIterator->first.getName() << "(" << finalPrintIterator->first.getYear() << ") - Weighted Score: " << finalPrintIterator->second << endl;
                                                                                                 ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h: In instantiation of ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = MovieBase::Movie]’:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_tree.h:1422:8:   required from ‘std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_hint_unique_pos(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, const key_type&) [with _Key = MovieBase::Movie; _Val = std::pair<const MovieBase::Movie, float>; _KeyOfValue = std::_Select1st<std::pair<const MovieBase::Movie, float> >; _Compare = std::less<MovieBase::Movie>; _Alloc = std::allocator<std::pair<const MovieBase::Movie, float> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const MovieBase::Movie, float> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = MovieBase::Movie]’
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_tree.h:1478:64:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, const _Val&) [with _Key = MovieBase::Movie; _Val = std::pair<const MovieBase::Movie, float>; _KeyOfValue = std::_Select1st<std::pair<const MovieBase::Movie, float> >; _Compare = std::less<MovieBase::Movie>; _Alloc = std::allocator<std::pair<const MovieBase::Movie, float> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const MovieBase::Movie, float> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const MovieBase::Movie, float> >]’
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_map.h:648:54:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::iterator, const value_type&) [with _Key = MovieBase::Movie; _Tp = float; _Compare = std::less<MovieBase::Movie>; _Alloc = std::allocator<std::pair<const MovieBase::Movie, float> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const MovieBase::Movie, float> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const MovieBase::Movie, float>]’
UserBase.cpp:124:71:   required from here
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: error: no match for ‘operator<’ (operand types are ‘const MovieBase::Movie’ and ‘const MovieBase::Movie’)
       { return __x < __y; }
                    ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_algobase.h:64:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/char_traits.h:39,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:40,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_pair.h:220:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_pair.h:220:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::pair<_T1, _T2>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_algobase.h:67:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/char_traits.h:39,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:40,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator<(const reverse_iterator<_Iterator>& __x,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_iterator.h:297:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::reverse_iterator<_Iterator>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_algobase.h:67:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/char_traits.h:39,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:40,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator<(const reverse_iterator<_IteratorL>& __x,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_iterator.h:347:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::reverse_iterator<_Iterator>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:52:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2569:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2569:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:52:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2581:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2581:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:52:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2593:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const _CharT* __lhs,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2593:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   mismatched types ‘const _CharT*’ and ‘MovieBase::Movie’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/map:60:0,
                 from UserBase.h:11,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_tree.h:917:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
     operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_tree.h:917:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/map:61:0,
                 from UserBase.h:11,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_map.h:979:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
     operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_map.h:979:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::map<_Key, _Tp, _Compare, _Alloc>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/map:62:0,
                 from UserBase.h:11,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_multimap.h:881:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
     operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_multimap.h:881:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::multimap<_Key, _Tp, _Compare, _Alloc>’
       { return __x < __y; }
                    ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/vector:64:0,
                 from MovieBase.h:11,
                 from UserBase.h:12,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_vector.h:1420:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
     operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_vector.h:1420:5: note:   template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:48:0,
                 from UserBase.h:10,
                 from UserBase.cpp:9:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_function.h:235:20: note:   ‘const MovieBase::Movie’ is not derived from ‘const std::vector<_Tp, _Alloc>’
       { return __x < __y; }
                    ^
nbproject/Makefile-Debug.mk:73: recipe for target 'build/Debug/Cygwin_4.x-Windows/UserBase.o' failed
make[2]: *** [build/Debug/Cygwin_4.x-Windows/UserBase.o] Error 1
make[2]: Leaving directory '/cygdrive/s/Production/Projects/CSE250/250-REGAN-PROJ2/NetflixChallenge'
nbproject/Makefile-Debug.mk:61: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/s/Production/Projects/CSE250/250-REGAN-PROJ2/NetflixChallenge'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)

3 个答案:

答案 0 :(得分:1)

不确定userbase.cpp中的第146行是什么,但你似乎在const对象上调用了非const成员函数。

答案 1 :(得分:0)

我猜是

vector<MovieBase::Movie>::iterator

是问题所在。如果您没有通过在代码顶部附近写<来声明std::vector<T>,那么它包含的#include <vector>可能会被解释为小于号。< / p>

答案 2 :(得分:0)

问题在于std::map<T>要求类型T配备operator<()(1-arg方法或2-arg自由函数),但是MovieBase::Movie std::map<T> 1}}类不提供一个。 {{1}}需要此运算符,因为它在内部将键值对存储在平衡二叉树中,这需要比较不同的键值。