使用 -> 时提供对象"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
在数据结构类的图形类上工作,我遇到了一个边缘对象的问题:
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;
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- C++:TypeDef使用元组
- 使用std::multimap迭代器创建std::list
- 从不同线程使用int64的不同字节安全吗
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用Google Mock来模拟gettimeofday()
- 如何使用默认参数等选择模板专业化
- 为什么使用 "this" 指针调用派生成员函数?
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用 "this" 关键字时的"Error: expression must have a pointer type"
- 使用可变宏时获得"Error: #28: expression must have a constant value "
- 如何使用 std::array 的默认参数? "array must be initialized with a brace-enclosed initializer"
- 使用 -> 时提供对象"must be a pointer-to-class type",使用 - 时提供"must be a class type"对象
- "error: '=' : left operand must be l-value" ?(在三元中使用赋值时:)
- 使用自动输入循环错误"auto must have an initializer"
- 使用未记录的库函数"Expression must have class type"
- 使用流时"expression must have a constant value"