处理数据结构类的图表类,我遇到了一个边缘对象的问题:
myGraph.nodes[a]->edges.back.n1 = myGraph.nodes[a];
myGraph.nodes[a]->edges.back->n2 = myGraph.nodes[b];
myGraph.nodes[a]->edges.back.cost = c;
如果我尝试对对象进行操作,就好像它是一个指针,它告诉我它必须是一个指向类的指针类型(C2227),如果我尝试将它作为一个类类型进行操作它告诉我它必须是一种类型(C2228)
我已经尝试了所有我知道如何做的事情,并在这个问题上与所有同行联系(我的教授无法联系到)。我不知道第三种类型或方式来引用成员。
任何问题都应该包含在main.cpp文件中我知道lab11代码工作正常。
它们都是指针,所以应该用 - >引用它们。运营商,但他们根本就没有。
的main.cpp
#include <iostream>
#include <fstream>
#include "lab11.cpp";
using namespace std;
int main() {
}
Graph writeGraph(string filename) {
ifstream myfile;
myfile.open(filename);
int numberofnodes;
myfile >> numberofnodes;
Graph myGraph;
for (int i = 0; i < numberofnodes; i++) {
myGraph.nodes.push_back(new Node(i));
}
int a = 0, b = 0, c = 0;
while (myfile >> a) {
if (a == -1) {
break;
}
myfile >> b >> c;
Edge *newEdge();
Node * temp = myGraph.nodes[a];
myGraph.nodes[a]->edges.push_back(new Edge());
myGraph.nodes[a]->edges.back.n1 = myGraph.nodes[a];
myGraph.nodes[a]->edges.back->n2 = myGraph.nodes[b];
myGraph.nodes[a]->edges.back.cost = c;
}
}
lab11.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
class Node;
class Edge {
public:
Edge() { n1 = 0; n2 = 0; inspanningtree = false; }
Node * n1;
Node * n2;
int cost;
bool inspanningtree;
};
class Node {
public:
Node(int nodeNumber)
{
this->nodeNumber = nodeNumber;
lastnum = -1;
}
int nodeNumber;
vector<Edge *> edges;
int lastnum;
};
class Graph {
public:
vector<Node *> nodes;
vector<Edge *> edges;
};
/*
void shellsortEdge( vector<Edge *> & a )
{
for( int gap = a.size( ) / 2; gap > 0; gap /= 2 )
for( int i = gap; i < a.size( ); ++i )
{
Edge * tmp = std::move( a[ i ] );
int j = i;
for( ; j >= gap && *tmp < *(a[ j - gap ]); j -= gap )
a[ j ] = std::move( a[ j - gap ] );
a[ j ] = std::move( tmp );
}
}*/
int glastnum = 0;
bool find(Node * current, Node * tofind, Node * from)
{
for (unsigned int i = 0; i < current->edges.size(); i++) {
if (current->edges[i]->inspanningtree) {
if (current->edges[i]->n1 != from && current != current->edges[i]->n1)//prob
{
if (current->edges[i]->n1->lastnum == glastnum) {
return true;
}
current->edges[i]->n1->lastnum = glastnum;
bool b = find(current->edges[i]->n1, tofind, current);
if (b == true)
return true;
}
if (current->edges[i]->n2 != from && current != current->edges[i]->n2)//prob
{
if (current->edges[i]->n2->lastnum == glastnum) {
return true;
}
current->edges[i]->n2->lastnum = glastnum;
bool b = find(current->edges[i]->n2, tofind, current);
if (b == true)
return true;
}
}
}
return false;
}
bool doesAddingThisMakeACycle(Graph & g, Edge * toBeAdded)
{
toBeAdded->inspanningtree = true;
glastnum++;
Node * n1 = toBeAdded->n1;
Node * n2 = toBeAdded->n2;
bool b = find(n1, n1, n1);
if (b) {
toBeAdded->inspanningtree = false;
return true;
}
glastnum++;
b = find(n2, n2, n2);
if (b) {
toBeAdded->inspanningtree = false;
return true;
}
toBeAdded->inspanningtree = false;
return false;
}
我讨厌来这里做作业帮助,因为我知道这是多么令人不满,但我只是出于其他选择我很乐意删除它,如果它不合适
答案 0 :(得分:1)
System.out.println(reader.nextDouble());
是back
的成员函数。您缺少函数调用。
std::vector
可替换地,
myGraph.nodes[a]->edges.back()->n1 = myGraph.nodes[a];
myGraph.nodes[a]->edges.back()->n2 = myGraph.nodes[b];
myGraph.nodes[a]->edges.back()->cost = c;