gpt4 book ai didi

c++ - 无法在 C++ 中创建对象

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:48 24 4
gpt4 key购买 nike

我一直在尝试解决图形问题,老师给了我这段代码。我不知道如何运行它。我的代码是:

#include <bits/stdc++.h>
using namespace std;

#define White 0
#define Gray 2
#define Black 3

int row, col;
int **Graph;

class Graph
{
private:
bool** adjacencyMatrix;
int vertexCount;
public:

Graph(int vertexCount)
{

this->vertexCount = vertexCount;
adjacencyMatrix = new bool*[vertexCount];

for (int i = 0; i < vertexCount; i++)
{
adjacencyMatrix[i] = new bool[vertexCount];
for (int j = 0; j < vertexCount; j++)
adjacencyMatrix[i][j] = false;
}
}

Graph(char filename[], int vertexCount)
{

this->vertexCount = vertexCount;
adjacencyMatrix = new bool*[vertexCount];

ifstream file;
file.open(filename, ios::in);

if( !file)
{
cout << "\nError: Cannot open file\n";
return;
}

if(file.is_open())
{
for (int i = 0; i < vertexCount; i++)
{

adjacencyMatrix[i] = new bool[vertexCount];

for (int j = 0; j < vertexCount; j++)
file>>adjacencyMatrix[i][j];
}
}
}


~Graph()
{
for (int i = 0; i < vertexCount; i++)
delete[] adjacencyMatrix[i];
delete[] adjacencyMatrix;
}

void runDFS(int u, int state[])
{

state[u] = Gray;

for (int v = 0; v < vertexCount; v++)

if (isEdge(u, v) && state[v] == White)

runDFS(v, state);

state[u] = Black;
cout<<u<<",";
}

void DFS()
{

int *state = new int[vertexCount];

for (int i = 0; i < vertexCount; i++)
state[i] = White;

runDFS(0, state);

delete [] state;

}

void display()
{

int u,v;

for(u=0; u<vertexCount; ++u)
{
cout << "\nadj[" << (char) (u+65) << "] -> ";
for(v=0; v<vertexCount; ++v)
{
cout << " " << adjacencyMatrix[u][v];
}
}
cout << "\n\n";
}

bool isEdge(int i, int j)
{
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
return adjacencyMatrix[i][j];
else
return false;
}

void removeEdge(int i, int j)
{
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
{
adjacencyMatrix[i][j] = false;
adjacencyMatrix[j][i] = false;
}
}

void addEdge(int i, int j)
{
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
{
adjacencyMatrix[i][j] = true;
adjacencyMatrix[j][i] = true;
}
}
};


int main()
{
Graph = new int*[row];
for (int i = 0; i < row; i++)
{
Graph[i] = new int[col];
}

for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Graph[i][j] = 0;
}
}

Graph A = new Graph("Hello.txt", 2); //This line is showing the error

}

但它向我显示错误:

D:\Fayaz\Undirected graph.cpp|157|error: expected ';' before 'A'|

谁能告诉我如何创建 Graph 类的对象并使用它的函数/方法?提前致谢...

最佳答案

这是您的代码的伪更正版本:

#include <iostream>
#include <fstream>
#include <map>
#include <vector>

enum class BWColor
{
White,
Gray,
Black
};

class Graph
{
private:
std::map<int, std::vector<int>> graph;
int vertexCount;

private:
void dfs(std::vector<BWColor>& visit, int u)
{
std::cout << u << " ";
visit[u] = BWColor::Gray;

for(std::vector<int>::iterator it = graph[u].begin(); it != graph[u].end(); ++it)
{
if(visit[*it] == BWColor::White)
{
dfs(visit, *it);
}
}

visit[u] = BWColor::Black;
}

public:
Graph(int vertexCount) : vertexCount(vertexCount) {}

Graph(const char* filename, int vertexCount) : Graph(vertexCount)
{
std::ifstream file;
file.open(filename, std::ios::in);

if(!file)
{
std::cerr << "Error: Cannot open file\n";
return;
}

if(file.is_open())
{
int k = 0;
int u = -1;

while(file >> k)
{
++u;

if(k == 0)
{
continue;
}

graph[u / vertexCount].push_back(u % vertexCount);
}
}
}


~Graph() {}

void runDFS(int u)
{
std::vector<BWColor> visit(vertexCount, BWColor::White);
dfs(visit, u);
}

friend std::ostream& operator<<(std::ostream& stream, const Graph& g);
};

std::ostream& operator<<(std::ostream& stream, const Graph& g)
{
for(std::map<int, std::vector<int>>::const_iterator it = g.graph.cbegin(); it != g.graph.cend(); ++it)
{
for(int i = 0; i < it->second.size(); ++i)
{
stream << '(' << it->first << ", " << it->second[i] << ")\n";
}
}

return stream;
}

int main()
{
Graph gA("graph.txt", 4);
std::cout << gA << "DFS: {";
gA.runDFS(2);
std::cout << "}\n";

return 0;
}

这里有一些建议:

  • 更喜欢 STL 容器(更安全)而不是 C 风格的容器;
  • 尽可能使用堆栈(新建和删除的成本很高)。因此忘记删除一个对象会导致内存泄漏

我正在使用第二个构造函数加载以下邻接矩阵,Graph(const char* filename, int vertexCount) :

0 1 0 0
0 0 1 0
1 0 0 1
0 0 1 1

然后,我将邻接矩阵转换为弧的集合 uv , 由 std::map<int, std::vector<int>> graph; 表示私有(private)成员(member)。

最后,我继续进行 DFS。

关于c++ - 无法在 C++ 中创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42917696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com