该表达式必须具有对象指针类型
the expression must have an object pointer type
这是我项目的代码。我有这样的程序,并且在图中填充数组时有问题。我认为新的问题,但我不明白到底是什么。有一个错误:表达式必须具有对象指针类型,我不知道该怎么办。我是C 的初学者
我试图使用一个。而不是 ->,但这是错误的
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
const int k = 3;
const int N = 1000;
struct Graph
{
struct Node *head[N];
};
struct Node
{
int dest;
int age;
string name;
string surname;
struct Node *next;
};
typedef Node* nodePtr;
struct Graph* createGraph(int );
void printGraph(struct Graph*);
void searchFor( int prefer[2*k][k], struct Graph* graph);
void createArray(struct Graph*, int arr[2*k][k]);
bool Preferences(int pref[2 * k][k], int w, int m, int m1);
int main()
{
int n;
int array[2 * k][k];
cout << "How many persons u want to have?" << endl;
cin >> n;
struct Graph *graph = createGraph(n);
printGraph(graph);
createArray(graph,array);
searchFor(array, graph);
return 0;
}
struct Graph* createGraph( int n)
{
int i,temp,j;
int b = n / 2;
int k, x = 0;
struct Graph *graph = new Graph;
for (i = 0; i < N; i++)
{
graph->head[i] = NULL;
}
srand(time(NULL));
for (i = 0; i < n; i++)
{
struct Node *newNode = new Node;
for ( k = 0; k < b; k++)
{
int m = b;
newNode->dest[k] = m;// here
b++;
}
for (int l = b; l < n; l++)
{
newNode->dest[l] = x; //here
x++;
}
for (int z = 0; z < b; z++)
{
if (z < b)
{
j = rand() % n + b;
temp = newNode->dest[z];
newNode->dest[z] = temp;
}
else
{
j = rand() % b + 0;
}
}
newNode->age = rand() % 90 + 1;
newNode->name = 'A' +(rand()%26);
newNode->surname = newNode->name = 'A' + (rand() % 26);
newNode->next = graph->head[i];
graph->head[i] = newNode;
}
return graph;
}
此语句:
newNode->dest[k] = m;
是非法的,因为dest
被声明为int
。您不能在整数上使用operator[]
。
您有两个主要的选择来解决此问题:
- 写
newNode->dest = m;
如果要将newNode->dest
的值更改为m
或 - 如果要保存多个值,则将
dest
更改为整数数组。
相关文章:
- 对象指针在c++中是如何工作的
- C++ 对象指针数组的复制构造函数
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 正确初始化和销毁对象指针的C++数组?
- 如何深度复制链表对象指针
- 对象指针 c++ 的全局向量错误
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 静态对象指针
- 正在将对象指针数组初始化为NULL
- 如何使用条件表达式返回对象指针?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- 使用C对象指针构建PyObject*
- 如何在使用对象指针时访问成员函数
- 静态强制转换允许转换对象指针,但不允许转换整数
- C++ abort() 在函数内的抽象类对象指针调用上
- 指向函数的对象指针
- 访问指向对象指针向量的指针的第一个元素?
- 如何将对象/指针正确存储到 Qlist 中
- 对象指针打印结果以相反的顺序进行