当使用 - >和“必须是类类型”时,给出“必须是类指针类型”的对象使用
问题描述:
处理数据结构类的图类并且遇到边缘问题对象的行:当使用 - >和“必须是类类型”时,给出“必须是类指针类型”的对象使用
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;
}
我讨厌来这里做作业帮助,因为我知道这是多么令人难以接受的,但我只是出于其他选项我会愉快地删除这个,如果不合适
答
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;
或者,
Edge* edge = myGraph.nodes[a]->edges.back();
edge->n1 = myGraph.nodes[a];
edge->n2 = myGraph.nodes[b];
edge->cost = c;
删除'边缘*新际();'' –
edges.back.n1'应该是'edges.back() - > n1',类似地对于'cost'和'n2'之后。 –
@ M.M'back'是'std :: vector'中的一个函数,不是成员变量。应该是'edges.back() - > n1'。 –