使用C ++结构的std :: sort和binary'='运算符问题

时间:2009-08-04 23:26:46

标签: c++ vector struct sorting

好的......我有这个结构和比较功能 -

struct Edge
{
          char point1;  
          char point2;  
          int weight;   

          bool operator<( const Edge& rhs ) const
          {
              return( weight < rhs.weight );
          }
}; //end Edge

bool compareEdge( const Edge& lhs, const Edge& rhs )
{
      return( lhs.weight < rhs.weight );
}

我有一个声明为...的向量

vector<Edge> edges;

最后,我尝试使用&lt;操作者...

sort( edges.begin(), edges.end() );

我在Visual Studio 2005中遇到以下错误......

------ Build started: Project: RadakovichLab6, Configuration: Debug Win32 ------ Compiling... graph.cpp c:\program files\microsoft visual studio 8\vc\include\algorithm(2981) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Edge' (or there is no acceptable conversion)
c:\documents and settings\jake\my documents\visual studio 2005\projects\radakovichlab6\graph.h(25): could be 'Edge &Edge::operator =(const Edge &)'
while trying to match the argument list '(const Edge, Edge)'
c:\program files\microsoft visual studio 8\vc\include\algorithm(2997) : see reference to function template instantiation 'void std::_Insertion_sort1<_BidIt,Edge>(_BidIt,_BidIt,_Ty*)' being compiled
with
[
    _BidIt=std::_Vector_const_iterator<Edge,std::allocator<Edge>>,
    _Ty=Edge
]
c:\program files\microsoft visual studio 8\vc\include\algorithm(3105) : see reference to function template instantiation 'void std::_Insertion_sort<_RanIt>(_BidIt,_BidIt)' being compiled
with
[
    _RanIt=std::_Vector_const_iterator<Edge,std::allocator<Edge>>,
    _BidIt=std::_Vector_const_iterator<Edge,std::allocator<Edge>>
]
c:\program files\microsoft visual studio 8\vc\include\algorithm(3112) : see reference to function template instantiation 'void std::_Sort<std::_Vector_const_iterator<_Ty,_Alloc>,__w64 int>(_RanIt,_RanIt,_Diff)' being compiled
with
[
    _Ty=Edge,
    _Alloc=std::allocator<Edge>,
    _RanIt=std::_Vector_const_iterator<Edge,std::allocator<Edge>>,
    _Diff=__w64 int
]
c:\documents and settings\jake\my documents\visual studio 2005\projects\radakovichlab6\graph.cpp(107) : see reference to function template instantiation 'void std::sort<std::_Vector_const_iterator<_Ty,_Alloc>>(_RanIt,_RanIt)' being compiled
with
[
    _Ty=Edge,
    _Alloc=std::allocator<Edge>,
    _RanIt=std::_Vector_const_iterator<Edge,std::allocator<Edge>>
] c:\program files\microsoft visual studio 8\vc\include\algorithm(2988) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Edge' (or there is no acceptable conversion)
c:\documents and settings\jake\my documents\visual studio 2005\projects\radakovichlab6\graph.h(25): could be 'Edge &Edge::operator =(const Edge &)'
while trying to match the argument list '(const Edge, const Edge)' c:\program files\microsoft visual studio 8\vc\include\algorithm(2989) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Edge' (or there is no acceptable conversion)
c:\documents and settings\jake\my documents\visual studio 2005\projects\radakovichlab6\graph.h(25): could be 'Edge &Edge::operator =(const Edge &)'
while trying to match the argument list '(const Edge, Edge)' Generating Code... Compiling... main.cpp Generating Code... Build log was saved at "file://c:\Documents and Settings\Jake\My Documents\Visual Studio 2005\Projects\RadakovichLab6\Debug\BuildLog.htm" RadakovichLab6 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

问题出在错误的第一行。我是否使用重载&lt;运算符,或将比较函数传递给std :: sort函数。我想,Edge结构的默认赋值运算符应该足够了,因为没有动态分配的内存。如果有人有任何见解,我将不胜感激。

完整代码......

        #include <list>
        #include <map>
        #include <queue>
        #include <vector>
        #include <algorithm>
        #include <iostream>

        using namespace std;

        //the Edge and aGraph together represent the adjacency list
        //representation of a graph.
        struct Edge
        {
            char point1;  //this represents the endpoint of an edge
            char point2;  
            int weight;   //this is the weight of the edge

            bool operator<( const Edge& rhs ) const
            {
                return( weight < rhs.weight );
            }
        }; //end Edge

        class Graph
        {
        public:
            //Default constructor
            Graph();

            //This method inputs an edge into a graph.
            void inputEdge( char pointA, char pointB, int wt )
            {
                //used to input the edges of the graph
            Edge anEdge;

            //prepare for insertion into list
            anEdge.point1 = pointA;
            anEdge.point2 = pointB;
            anEdge.weight = wt;

            edges.push_back( anEdge );

            //insert edge into the adjacency list.
            aGraph[pointA].push_front( pointB );

            //insert the opposite direction into the
            //adjacency list.
            aGraph[pointB].push_front( pointA )
             }

             //This...
             void bfs();

            //This prints a graph and is used only for debugging purposes.
            void printGraph() const
            {

             list<char>::const_iterator listIter;
             map<char, list<char>>::const_iterator mapIter;

             for( mapIter = aGraph.begin(); 
              mapIter != aGraph.end();
              mapIter++ )
             {
                for( listIter = mapIter->second.begin();
                         listIter != mapIter->second.end();
                         listIter++ )
               {
                     cout << mapIter->first << " " << *listIter << endl;
               } //end for
             } //end for


             sort( edges.begin(), edges.end() );

             vector<Edge>::const_iterator vectIt;

             for( vectIt = edges.begin();
                  vectIt != edges.end();
                  vectIt++ )
             {
                cout << vectIt->point1 << " " << vectIt->point2 << " " << vectIt->weight << endl;
              } //end for
         } //end printGraph


        private:
            //This is the adjacency list
            map<char, list<char>> aGraph;

            //This is a list of edges and their weights.
            vector<Edge> edges;
        };  //end Graph

int main()
{

    Graph myGraph;

    myGraph.inputEdge( 'O', 'A', 2 );
    myGraph.inputEdge( 'O', 'B', 5 );
    myGraph.inputEdge( 'O', 'C', 4 );
    myGraph.inputEdge( 'A', 'B', 2 );
    myGraph.inputEdge( 'A', 'D', 7 );
    myGraph.inputEdge( 'B', 'D', 4 );
    myGraph.inputEdge( 'B', 'E', 3 );
    myGraph.inputEdge( 'C', 'B', 1 );
    myGraph.inputEdge( 'C', 'E', 4 );
    myGraph.inputEdge( 'E', 'D', 1 );
    myGraph.inputEdge( 'D', 'T', 5 );
    myGraph.inputEdge( 'E', 'T', 7 );
    myGraph.inputEdge( 'G', 'Z', 8 );

    myGraph.printGraph();

    cout << endl << endl;

    system("PAUSE");
    return 0;
} //end main

这是代码......

5 个答案:

答案 0 :(得分:4)

您正在使用无法排序的const vector。 (或改变了。)

这是因为您的功能是const

void printGraph() const

删除const,以便修改(并因此排序)类的成员。

答案 1 :(得分:3)

以下代码在g ++ 4.4.0下编译:

#include <algorithm>
#include <vector>
using namespace std;

struct Edge {
    char point1;  
    char point2;  
    int weight;   

    bool operator<( const Edge& rhs ) const {
        return( weight < rhs.weight );
    }
}; 

bool compareEdge( const Edge& lhs, const Edge& rhs ) {
      return( lhs.weight < rhs.weight );
}

int main() {
    vector <Edge> edges;
    sort( edges.begin(), edges.end() );
}

请使用您的编译器进行测试。一般来说,如果你和其他人发布了这样的小型完整程序,这些程序可以很容易地进行测试,并且说明问题(或者在这种情况下缺少它们),那将会很有帮助。

答案 2 :(得分:0)

上面的代码编译得很好,在尝试将内容分配给const Edge时,看起来你有一个赋值问题。

您不需要定义赋值运算符,但不能将某些内容赋给const Edge。

答案 3 :(得分:0)

我认为您使用std::sort而不是std::vector<Edge>::const_iterator拨打std::vector<Edge>::iterator,可能在graph.cpp第107行。

看到代码之后:迭代器确实是不变的。 edgesGraph的成员变量,printGraph是const方法。因此,无法在edges内修改printGraph,因为const方法无法修改Graph对象。排序edges会对其进行修改,因此会导致编译错误。

答案 4 :(得分:0)

printGraph函数被声明为Graph的const成员函数,但您正在尝试修改成员变量(edge vector)。从printGraph声明中删除const,代码将编译。

或者制作边缘矢量的局部副本并在打印之前对其进行排序,因为注释状态此功能仅用于调试性能不应该是主要关注点。对于一个名为printGraph的函数来说,无论如何都要修改边缘向量,这有点误导。