这是我的项目的一段代码。我有这样一个程序,在图表中填充数组时遇到问题。我认为这是新问题,但我不完全了解。发生错误: 该表达式必须具有对象指针类型,但我不知道该如何处理。我是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;
}
答案 0 :(得分:0)
此声明:
newNode->dest[k] = m;
是非法的,因为dest
被声明为int
。您不能在整数上使用operator[]
。
您有两个主要解决方法:
newNode->dest = m;
的值更改为newNode->dest
,请写m
,或者dest
更改为整数数组。