该表达式必须具有对象指针类型

the expression must have an object pointer type

本文关键字:对象 指针 类型 表达式      更新时间:2023-10-16

这是我项目的代码。我有这样的程序,并且在图中填充数组时有问题。我认为新的问题,但我不明白到底是什么。有一个错误:表达式必须具有对象指针类型,我不知道该怎么办。我是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更改为整数数组。