在发现编译器不知道如何查找的错误时,C++ 技能并不那么敏锐。显然没有缺少';' 之前 '*'。也许是它不喜欢包含语句的情况之一。有人可以帮助找到错误并解释为什么会抛出它吗?谢谢!
主文件
#include <iostream>
#include "Grid.h"
using namespace std;
int main()
{
Grid myGrid(15, 15, 15, 3);
myGrid.PrintGrid();
cout << endl << "The node closest to <0,0> is " << myGrid.GetNodeClosestToOrigin()->letter << "." << endl;
system("pause");
return 0;
}
边缘.h
#pragma once
#include "Node.h"
using namespace std;
class Edge
{
public:
Node* destination;
double weight;
Edge(Node* destination, double weight);
~Edge(void);
};
边缘.cpp
#include "Edge.h"
#include "Node.h"
using namespace std;
Edge::Edge(Node* destination, double weight)
{
this->destination = destination;
this->weight = weight;
}
Edge::~Edge(void)
{
}
节点.h
#pragma once
#include <vector>
#include "Node.h"
#include "Edge.h"
using namespace std;
class Node
{
public:
int x;
int y;
char letter;
vector<Edge> edges;
Node(void);
~Node(void);
void AddEdge(Node* destination);
bool ContainsEdgeWithDestination(Node* destination);
double GetDistanceTo(int x, int y);
double GetDistanceTo(Node* node);
};
节点.cpp
#include "Node.h"
using namespace std;
Node::Node(void)
{
}
Node::~Node(void)
{
}
void Node::AddEdge(Node* destination)
{
double weight = GetDistanceTo(destination);
Edge myEdge(destination, weight);
this->edges.push_back(myEdge);
}
bool Node::ContainsEdgeWithDestination(Node* destination)
{
for (int i = 0; i < edges.size(); i++)
{
if (edges[i].destination == destination)
return true;
}
return false;
}
double Node::GetDistanceTo(int x, int y)
{
double a = ((double)x - (double)this->x)*((double)x - (double)this->x);
double b = ((double)y - (double)this->y)*((double)y - (double)this->y);
return sqrt(a + b);
}
double Node::GetDistanceTo(Node* node)
{
return GetDistanceTo(node->x, node->y);
}
更新:
边缘.h
#pragma once
#include "Node.h"
using namespace std;
class Grid
{
private:
Node *nodes;
int gridWidth;
int gridHeight;
int numNodes;
int neighborsPerNode;
double GetDistance(int x1, int y1, int x2, int y2);
public:
Grid(int gridWidth, int gridHeight, int numNodes, int neighborsPerNode);
~Grid();
void Initialize();
bool IsNodeAtLocation(int x, int y);
Node* GetNodeAtLocation(int x, int y);
Node* GetNodeClosestToOrigin();
void PrintGrid();
};
网格.cpp
#include <iostream>
#include <random>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "Grid.h"
#include "Node.h"
using namespace std;
Grid::Grid(int gridWidth, int gridHeight, int numNodes, int neighborsPerNode)
{
this->gridWidth = gridWidth;
this->gridHeight = gridHeight;
this->numNodes = numNodes;
this->neighborsPerNode = neighborsPerNode;
nodes = new Node[numNodes];
Initialize();
}
Grid::~Grid(void)
{
}
void Grid::Initialize()
{
srand(time(NULL));
// Create nodes.
for (int i = 0; i < numNodes; i++)
{
int x;
int y;
do
{
// Randomize a position for a new node.
x = rand() % gridWidth;
y = rand() % gridHeight;
} while (IsNodeAtLocation(x, y));
// Position is available to place new node.
nodes[i].x = x;
nodes[i].y = y;
nodes[i].letter = 65 + i;
}
// Create edges.
int edgeCount[15];
for (int i = 0; i < numNodes; i++) edgeCount[i] = 0;
for (int i = 0; i < numNodes; i++)
{
if (edgeCount[i] < neighborsPerNode)
{
for (int j = edgeCount[i]; j < neighborsPerNode; j++)
{
// Randomize a node to create an edge to.
int destination;
while(true)
{
destination = rand() % numNodes;
// Check if destination is current node.
if (destination == i) continue;
// Check if destination already has maximum amount of edges.
if (edgeCount[destination] >= neighborsPerNode) continue;
// Check if node already has an edge with this destination.
if (nodes[i].ContainsEdgeWithDestination(&nodes[destination])) continue;
// Should be ok. Create new edge.
nodes[i].AddEdge(&nodes[destination]);
nodes[destination].AddEdge(&nodes[i]);
edgeCount[destination]++;
break;
}
}
}
}
cout << "We're in business" << endl;
}
bool Grid::IsNodeAtLocation(int x, int y)
{
for (int i = 0; i < numNodes; i++)
{
if (nodes[i].x == x && nodes[i].y == y)
return true;
}
return false;
}
Node* Grid::GetNodeAtLocation(int x, int y)
{
for (int i = 0; i < numNodes; i++)
{
if (nodes[i].x == x && nodes[i].y == y)
return &nodes[i];
}
return NULL;
}
Node* Grid::GetNodeClosestToOrigin()
{
double smallestDistance = 999999.0;
int indexOfNode;
for (int i = 0; i < numNodes; i++)
{
double distance = GetDistance(0, 0, nodes[i].x, nodes[i].y);
if (distance < smallestDistance)
{
smallestDistance = distance;
indexOfNode = i;
}
}
return &nodes[indexOfNode];
}
double Grid::GetDistance(int x1, int y1, int x2, int y2)
{
double a = ((double)x2 - (double)x1)*((double)x2 - (double)x1);
double b = ((double)y2 - (double)y1)*((double)y2 - (double)y1);
return sqrt(a + b);
}
void Grid::PrintGrid()
{
for (int i = 0; i < gridWidth * 2 + 2; i++)
cout << "-";
cout << endl;
for (int y = 0; y < gridHeight; y++)
{
cout << "|";
for (int x = 0; x < gridWidth; x++)
{
Node* myNode = GetNodeAtLocation(x, y);
if (myNode != NULL)
cout << myNode->letter << " ";
else
cout << " ";
}
cout << "|" << endl;
}
for (int i = 0; i < gridWidth * 2 + 2; i++)
cout << "-";
cout << endl;
}
错误列表: