首页 > 解决方案 > 如何修复二元运算符错误?

问题描述

我必须编写使用邻接表来表示图形的代码。我尝试将我的代码设置为使用 std::list 但现在这给了我错误说 Error C2679 binary '=': no operator found which take a right-hand operand of type 'int' (或者没有可接受的转换)在我的代码的多行上。我对如何让我的代码使用邻接列表而没有所有这些问题感到困惑。

#include "GraphInterface.h"
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include<list>

#ifndef GRAPH_TWO
#define GRAPH_TWO

template<class LabelType>
class GraphTwo : public GraphInterface<LabelType>
{
private:
    // Define maximum number of nodes
    static const int size = 10;
    std::list <int> adj[size][size];
    std::list<bool>visited[size];

public:
    GraphTwo();

    // Get the number of vertices
    int getNumVertices() const;

    // Get the number of the edges
    int getNumEdges() const;


    // Creates an undirected edge in this graph between two vertices
    // that have the given labels.If such vertices do not exist, creates
    // themand adds them to the graph before creating the edge
    bool add(LabelType start, LabelType end, int edgeWeight);


    // Removes an edge from this graph. If a vertex has no other edges,
    // it is removed from the graph since this is a connected graph.
    bool remove(LabelType start, LabelType end);


    // Gets the weight of an edge in this graph.
    int getEdgeWeight(LabelType start, LabelType end) const;


    // Performs a depth - first search of this graph beginning at the given
    // vertex and calls a given function once for each vertex visited.
    void depthFirstTraversal(LabelType start, void visit(LabelType&));


    // Performs a breadth - first search of this graph beginning at the given
    // vertex and calls a given function once for each vertex visited.
    void breadthFirstTraversal(LabelType start, void visit(LabelType&));
};

template<class LabelType>
GraphTwo<LabelType>::GraphTwo()
{}

template<class LabelType>
int GraphTwo<LabelType>::getNumVertices() const
{
    return size;
}

template<class LabelType>
int GraphTwo<LabelType>::getNumEdges() const
{
    int edgeCount = 0;
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            if (adj[i][j] != 0)
                ++edgeCount;

    return edgeCount / 2;
}

template<class LabelType>
bool GraphTwo<LabelType>::add(LabelType start, LabelType end, int edgeWeight)
{
    adj[start][end] = edgeWeight; //error here 
    adj[end][start] = edgeWeight; //error here
    return true;
}

template<class LabelType>
bool GraphTwo<LabelType>::remove(LabelType start, LabelType end)
{
    adj[start][end] = 0; // error here
    adj[end][start] = 0; // error here
    return true;
}

template<class LabelType>
int GraphTwo<LabelType>::getEdgeWeight(LabelType start, LabelType end) const
{
    return adj[start][end];
}

template<class LabelType>
void GraphTwo<LabelType>::depthFirstTraversal(LabelType start, void visit(LabelType&))
{
    // Visit the current node
    visit(start);

    // Mark the current node as visited
    visited[start] = true;

    // For all other nodes
    for (int i = 0; i < size; ++i) {
        if (adj[start][i] != 0 && (!visited[i]))
            depthFirstTraversal(i, visit);
    }
}

template<class LabelType>
void GraphTwo<LabelType>::breadthFirstTraversal(LabelType start, void visit(LabelType&))
{
    // Vector that contains the adjacent nodes
    std::vector<LabelType> alist;
    alist.push_back(start);

    // Mark current node as visited
    visited[start] = true; // error here

    int check;
    while (!alist.empty()) {
        check = alist[0];

        // Print node
        visit(check);
        alist.erase(alist.begin());

        // Every vertex adjacent
        for (int i = 0; i < size; ++i) {
            if (adj[check][i] != 0 && (!visited[i])) {
                // Add node to the queue
                alist.push_back(i);

                // Mark next node as visited
                visited[i] = true; // error here
            }
        }
    }
    // Reset visited as all false
    for (int i = 0; i < size; ++i)
        visited[i] = false; // error here
}


#endif

标签: c++

解决方案


std::list <int> adj[size][size];

这不是你想的那样。adj大小列表的大小数组的数组。

这使得adj[start][end]有类型std::list<int>。这同样适用于您的所有列表。去找一个关于如何使用的好文档,std::list不要试图猜测C++ 语法,学习它。


推荐阅读