此程序假設在子進程和父進程之間創建共享內存,其中子進程將特定長度(參數)的斐波那契數列保存到其中父進程將其吐出。它也假設連接和分離共享內存。一切似乎功能,除了我得到這個錯誤的事實:'shared_data * shm'中的'operator *'不匹配
proj2.cpp:40: error: no match for 'operator*' in 'shared_data *shm' error
任何幫助嗎?代碼如下。
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#define MAX_SEQUENCE 10
struct shared_data{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
} shared_data;
using namespace std;
char * shm;
int Fibonacci(int n){
int first = 0, second = 1, temp = 0;
shared_data.fib_sequence[0] = first;
shared_data.fib_sequence[1] = second;
for(int i = 2; i<=n; i++){
temp = first + second;
shared_data.fib_sequence[i] = temp;
first = second;
second = temp;
}
return 0;
}
int main(int argc, char *argv[])
{
pid_t pid;
int seg_id;
const int shd = 4096;
seg_id = shmget(IPC_PRIVATE, shd, S_IRUSR | S_IWUSR);
shared_data *shm = shmat(seg_id, NULL, 0);
int number = atoi(argv[1]);
if(number < 0 || number > 10){
cout << "Invalid number. Please enter a number greater than 0 \n";
return(1);
}
shared_data.sequence_size = number;
pid = fork();
if(pid == 0)
Fibonacci(number);
else{
waitpid(pid,0,0);
for (int i = 0; i <= shared_data.sequence_size; i++)
cout << shared_data.fib_sequence[i];
cout << "\n";
}
return 0;
}
當我使用typedef我得到這些錯誤代碼「預期之前的非限定id」。令牌「和」預期的主要表達之前「。令牌」 – 2013-03-04 03:34:33