Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ';这';无法在常量表达式错误(C+;+;)中使用此->;指针变量_C++_Error Handling - Fatal编程技术网

C++ ';这';无法在常量表达式错误(C+;+;)中使用此->;指针变量

C++ ';这';无法在常量表达式错误(C+;+;)中使用此->;指针变量,c++,error-handling,C++,Error Handling,我收到这个错误消息,我知道这是因为数组中的参数必须是常量,但我不知道如何安排我的'this->V;保持不变。有什么帮助吗?我提供了大部分代码,这样指针变量的功能在整个程序中都很明显 // A class that represents an undirected graph class Graph { int V; // number of vertices list<int> *adj; // A dynamic array of adjacency lists int

我收到这个错误消息,我知道这是因为数组中的参数必须是常量,但我不知道如何安排我的'this->V;保持不变。有什么帮助吗?我提供了大部分代码,这样指针变量的功能在整个程序中都很明显

// A class that represents an undirected graph
class Graph
{
int V;    // number of vertices
list<int> *adj;    // A dynamic array of adjacency lists
int *in;
public:
// Constructor and destructor
Graph(int V);
~Graph() { delete[] adj; delete[] in; }

// function to add an edge to graph
void addEdge(int v, int w) { adj[v].push_back(w);  (in[w])++; }

// Method to check if this graph is Eulerian or not
bool isEulerianCycle();

// Method to check if all non-zero degree vertices are connected
bool isSC();

// Function to do DFS starting from v. Used in isConnected();
void DFSUtil(int v, bool visited[]);

Graph getTranspose();
};

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
in = new int[V];
for (int i = 0; i < V; i++)
    in[i] = 0;
}

/* This function returns true if the directed graph has an eulerian
cycle, otherwise returns false  */
bool Graph::isEulerianCycle()
{
// Check if all non-zero degree vertices are connected
if (isSC() == false)
    return false;

// Check if in degree and out degree of every vertex is same
for (int i = 0; i < V; i++)
    if (adj[i].size() != in[i])
        return false;

return true;
}

// A recursive function to do DFS starting from v
void Graph::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and print it
visited[v] = true;

// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
    if (!visited[*i])
        DFSUtil(*i, visited);
}

// Function that returns reverse (or transpose) of this graph
// This function is needed in isSC()
Graph Graph::getTranspose()
{
Graph g(V);
for (int v = 0; v < V; v++)
{
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
    {
        g.adj[*i].push_back(v);
        (g.in[v])++;
    }
}
return g;
}

// This function returns true if all non-zero degree vertices of 
// graph are strongly connected
bool Graph::isSC()
{
// Mark all the vertices as not visited (For first DFS)
bool visited[V]; // error: 'this' cannot be used in a constant expression
for (int i = 0; i < V; i++)
    visited[i] = false;

// Find the first vertex with non-zero degree
int n;
for (n = 0; n < V; n++)
    if (adj[n].size() > 0)
        break;

// Do DFS traversal starting from first non zero degree vertex.
DFSUtil(n, visited);

// If DFS traversal doesn't visit all vertices, then return false.
for (int i = 0; i < V; i++)
    if (adj[i].size() > 0 && visited[i] == false)
        return false;

// Create a reversed graph
Graph gr = getTranspose();

// Mark all the vertices as not visited (For second DFS)
for (int i = 0; i < V; i++)
    visited[i] = false;

// Do DFS for reversed graph starting from first vertex.
// Staring Vertex must be same starting point of first DFS
gr.DFSUtil(n, visited);

// If all vertices are not visited in second DFS, then
// return false
for (int i = 0; i < V; i++)
    if (adj[i].size() > 0 && visited[i] == false)
        return false;

return true;
}
//表示无向图的类
类图
{
int V;//顶点数
list*adj;//邻接列表的动态数组
int*in;
公众:
//构造函数和析构函数
图形(INTV);
~Graph(){delete[]adj;delete[]in;}
//函数将边添加到图形中
无效的加法(intv,intw){adj[v]。向后推(w);(in[w])++}
//方法检查此图是否为欧拉图
bool-isEulerianCycle();
//方法检查是否连接了所有非零度顶点
bool-isSC();
//从v开始执行DFS的函数。在isConnected()中使用;
void DFSUtil(int v,bool已访问[]);
图getTranspose();
};
图形::图形(intv)
{
这个->V=V;
adj=新列表[V];
in=新的整数[V];
对于(int i=0;i0)
打破
//从第一个非零度顶点开始执行DFS遍历。
DFSUtil(n,已访问);
//如果DFS遍历未访问所有顶点,则返回false。
对于(int i=0;i0&&访问[i]==false)
返回false;
//创建反向图形
图gr=getTranspose();
//将所有顶点标记为未访问(对于第二个DFS)
对于(int i=0;i0&&访问[i]==false)
返回false;
返回true;
}

C++不支持大小非常量的数组。(非常量表示编译时未知的内容)。因此,生产线出现错误:

bool visited[V];
重写代码并坚持使用std容器(std::vector、std::list)

类图
{
int V;//顶点数
vector adj;//邻接列表的动态数组
矢量输入;
//等等。。。
//您可以像普通数组一样访问这些元素(如[3]=5等)
图形::图形(intv)
{
这个->V=V;
调整大小(V);
in.调整大小(V);
//等
病媒访问(五);
希望这将为修复代码和克服当前错误提供一个良好的开端

编辑

类图
{
vector adj;//邻接列表的动态数组
矢量输入;
公众:
图形(INTV);
~Graph(){}
//函数将边添加到图形中
无效的加法(intv,intw){adj[v]。向后推(w);(in[w])++}
//方法检查此图是否为欧拉图
bool-isEulerianCycle();
//方法检查是否连接了所有非零度顶点
bool-isSC();
//从v开始执行DFS的函数。在isConnected()中使用;
无效DFSUtil(整数v、向量和访问量);
图getTranspose();
};
图形::图形(intv)
{
调整大小(V);
in.调整大小(V,0);
}
/*如果有向图具有欧拉曲线,则此函数返回true
循环,否则返回false*/
布尔图::isEulerianCycle()
{
//检查是否连接了所有非零度顶点
如果(isSC()==false)
返回false;
//检查每个顶点的入度和出度是否相同
对于(大小i=0;iclass Graph
{
    int V;    // number of vertices
    vector<list<int>> adj;    // A dynamic array of adjacency lists
    vector<int> in;
    // etc...
    // and you can access the elements just like a normal array (in[3] = 5, etc...)

Graph::Graph(int V)
{
    this->V = V;
    adj.Resize(V);
    in.Resize(V);
    // etc

vector<bool> visited(V);
class Graph
{
    vector<list<int>> adj;     // A dynamic array of adjacency lists
    vector<int> in;
public:
    Graph(int V);
    ~Graph() { }
    // function to add an edge to graph
    void addEdge(int v, int w) { adj[v].push_back(w);  (in[w])++; }
    // Method to check if this graph is Eulerian or not
    bool isEulerianCycle();
    // Method to check if all non-zero degree vertices are connected
    bool isSC();
    // Function to do DFS starting from v. Used in isConnected();
    void DFSUtil(int v, vector<bool>& visited);

    Graph getTranspose();
};

Graph::Graph(int V)
{
    adj.resize(V);
    in.resize(V, 0);
}

/* This function returns true if the directed graph has an eulerian
cycle, otherwise returns false  */
bool Graph::isEulerianCycle()
{
    // Check if all non-zero degree vertices are connected
    if (isSC() == false)
        return false;

    // Check if in degree and out degree of every vertex is same
    for (size_t i = 0; i < adj.size(); i++)
        if (adj[i].size() != in[i])
            return false;
    return true;
}

// A recursive function to do DFS starting from v
void Graph::DFSUtil(int v, vector<bool>& visited)
{
    // Mark the current node as visited and print it
    visited[v] = true;

    // Recur for all the vertices adjacent to this vertex
    for (auto i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i, visited);
}




Graph Graph::getTranspose()
{
    Graph g(adj.size());
    for (int v = 0; v < adj.size(); v++)
    {
        // Recur for all the vertices adjacent to this vertex
        //list<int>::iterator i;
        for (auto i = adj[v].begin(); i != adj[v].end(); ++i)
        {
            g.adj[*i].push_back(v);
            (g.in[v])++;
        }
    }
    return g;
}
// This function returns true if all non-zero degree vertices of 
// graph are strongly connected
bool Graph::isSC()
{
    // Mark all the vertices as not visited (For first DFS)
    vector<bool> visited(adj.size(), false);

    // Find the first vertex with non-zero degree
    int n;
    for (n = 0; n < adj.size(); n++)
        if (adj[n].size() > 0)
            break;

    // Do DFS traversal starting from first non zero degree vertex.
    DFSUtil(n, visited);

    // If DFS traversal doesn't visit all vertices, then return false.
    for (int i = 0; i < adj.size(); i++)
        if (adj[i].size() > 0 && visited[i] == false)
            return false;

    // Create a reversed graph
    Graph gr = getTranspose();

    // Mark all the vertices as not visited (For second DFS)
    visited.resize(visited.size(), false);

    // Do DFS for reversed graph starting from first vertex.
    // Staring Vertex must be same starting point of first DFS
    gr.DFSUtil(n, visited);

    // If all vertices are not visited in second DFS, then
    // return false
    for (int i = 0; i < adj.size(); i++)
        if (adj[i].size() > 0 && visited[i] == false)
            return false;

    return true;
}
bool visited[V];
std::vector<bool> visited(V);