使用 -> 时提供对象"must be a pointer-to-class type",使用 - 时提供"must be a class type"对象

Object giving "must be a pointer-to-class type" when using -> and "must be a class type" when using

本文关键字:使用 must be type 对象 class gt pointer-to-class      更新时间:2023-10-16

在数据结构类的图形类上工作,我遇到了一个边缘对象的问题:

        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;
}

我讨厌来这里找作业帮助,因为我知道这是多么不受欢迎,但我只是没有其他选择,如果不合适,我会很高兴删除这个

backstd::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;