Note that there are some explanatory texts on larger screens.

plurals
  1. POCompile error that should be easy for C++ expert
    text
    copied!<p>C++ skills are not that sharp when it comes to spotting errors that the compiler does not know how to find. Obviously there is not a missing ';' before the '*'. Perhaps one of those situations where it doesn't like an include statement. Can someone please help find the error(s) and explain why it's throwing it? Thank you!</p> <p>Main.cpp</p> <pre><code>#include &lt;iostream&gt; #include "Grid.h" using namespace std; int main() { Grid myGrid(15, 15, 15, 3); myGrid.PrintGrid(); cout &lt;&lt; endl &lt;&lt; "The node closest to &lt;0,0&gt; is " &lt;&lt; myGrid.GetNodeClosestToOrigin()-&gt;letter &lt;&lt; "." &lt;&lt; endl; system("pause"); return 0; } </code></pre> <p>Edge.h</p> <pre><code>#pragma once #include "Node.h" using namespace std; class Edge { public: Node* destination; double weight; Edge(Node* destination, double weight); ~Edge(void); }; </code></pre> <p>Edge.cpp</p> <pre><code>#include "Edge.h" #include "Node.h" using namespace std; Edge::Edge(Node* destination, double weight) { this-&gt;destination = destination; this-&gt;weight = weight; } Edge::~Edge(void) { } </code></pre> <p>Node.h</p> <pre><code>#pragma once #include &lt;vector&gt; #include "Node.h" #include "Edge.h" using namespace std; class Node { public: int x; int y; char letter; vector&lt;Edge&gt; edges; Node(void); ~Node(void); void AddEdge(Node* destination); bool ContainsEdgeWithDestination(Node* destination); double GetDistanceTo(int x, int y); double GetDistanceTo(Node* node); }; </code></pre> <p>Node.cpp</p> <pre><code>#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-&gt;edges.push_back(myEdge); } bool Node::ContainsEdgeWithDestination(Node* destination) { for (int i = 0; i &lt; 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-&gt;x)*((double)x - (double)this-&gt;x); double b = ((double)y - (double)this-&gt;y)*((double)y - (double)this-&gt;y); return sqrt(a + b); } double Node::GetDistanceTo(Node* node) { return GetDistanceTo(node-&gt;x, node-&gt;y); } </code></pre> <p>UPDATE:</p> <p>Edge.h</p> <pre><code>#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(); }; </code></pre> <p>Grid.cpp</p> <pre><code>#include &lt;iostream&gt; #include &lt;random&gt; #include &lt;math.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include "Grid.h" #include "Node.h" using namespace std; Grid::Grid(int gridWidth, int gridHeight, int numNodes, int neighborsPerNode) { this-&gt;gridWidth = gridWidth; this-&gt;gridHeight = gridHeight; this-&gt;numNodes = numNodes; this-&gt;neighborsPerNode = neighborsPerNode; nodes = new Node[numNodes]; Initialize(); } Grid::~Grid(void) { } void Grid::Initialize() { srand(time(NULL)); // Create nodes. for (int i = 0; i &lt; 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 &lt; numNodes; i++) edgeCount[i] = 0; for (int i = 0; i &lt; numNodes; i++) { if (edgeCount[i] &lt; neighborsPerNode) { for (int j = edgeCount[i]; j &lt; 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] &gt;= neighborsPerNode) continue; // Check if node already has an edge with this destination. if (nodes[i].ContainsEdgeWithDestination(&amp;nodes[destination])) continue; // Should be ok. Create new edge. nodes[i].AddEdge(&amp;nodes[destination]); nodes[destination].AddEdge(&amp;nodes[i]); edgeCount[destination]++; break; } } } } cout &lt;&lt; "We're in business" &lt;&lt; endl; } bool Grid::IsNodeAtLocation(int x, int y) { for (int i = 0; i &lt; numNodes; i++) { if (nodes[i].x == x &amp;&amp; nodes[i].y == y) return true; } return false; } Node* Grid::GetNodeAtLocation(int x, int y) { for (int i = 0; i &lt; numNodes; i++) { if (nodes[i].x == x &amp;&amp; nodes[i].y == y) return &amp;nodes[i]; } return NULL; } Node* Grid::GetNodeClosestToOrigin() { double smallestDistance = 999999.0; int indexOfNode; for (int i = 0; i &lt; numNodes; i++) { double distance = GetDistance(0, 0, nodes[i].x, nodes[i].y); if (distance &lt; smallestDistance) { smallestDistance = distance; indexOfNode = i; } } return &amp;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 &lt; gridWidth * 2 + 2; i++) cout &lt;&lt; "-"; cout &lt;&lt; endl; for (int y = 0; y &lt; gridHeight; y++) { cout &lt;&lt; "|"; for (int x = 0; x &lt; gridWidth; x++) { Node* myNode = GetNodeAtLocation(x, y); if (myNode != NULL) cout &lt;&lt; myNode-&gt;letter &lt;&lt; " "; else cout &lt;&lt; " "; } cout &lt;&lt; "|" &lt;&lt; endl; } for (int i = 0; i &lt; gridWidth * 2 + 2; i++) cout &lt;&lt; "-"; cout &lt;&lt; endl; } </code></pre> <p>Error List: <img src="https://i.stack.imgur.com/thGw8.jpg" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload