简体   繁体   English

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

[英]the expression must have an object pointer type

This is a piece of code for my project. 这是我项目的一段代码。 I have such a program and I have a problem with filling an array in the graph. 我有这样一个程序,我在图中填充数组有问题。 I think that the problem with new, but I don't understand what exactly. 我认为新问题,但我不明白到底是什么。 There is an error: the expression must have an object pointer type and I don't know what to do with it. 有一个错误:表达式必须有一个对象指针类型,我不知道如何处理它。 I'm a beginner in C++ 我是C ++的初学者

I tried to use a . 我试过用一个。 instead of -> but it was wrong 而不是 - >但它错了

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

This statement: 这个说法:

newNode->dest[k] = m;

is illegal because dest was declared as an int . 是非法的,因为dest被声明为int You can't use operator[] on an integer. 您不能在整数上使用operator[]

You have two main options to fix this: 您有两个主要选项来解决此问题:

  • Write newNode->dest = m; newNode->dest = m; if you want to change the value of newNode->dest to m , or 如果要将newNode->dest的值更改为m ,或
  • Change dest to an integer array if you want multiple values to be held in it. 如果要在其中保存多个值,请将dest更改为整数数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM