我正在为类编写代码,但是我不断收到-预期为'(',用于函数式强制类型转换或类型构造-错误,提示说cout << void enterStolenMoonPies(int * array,int numDays)< 这是标题: 这是函数文件:#include <iostream>
#include "Moonpie.h"
//#include "Functions.cpp"
using namespace std;
int* makeArray(int days);
void enterStolenMoonPies(int* array, int numDays);
int* totalMoonPies(int* array, int numDays);
float averageMoonPies(int numDays, int sum);
int* highestMoonPies(int* array, int numDays);
int* lowestMoonPies(int* array, int numDays);
int main()
{
int days;
cout <<"How many days did Jane steal moon pies?: ";
cin >> days;
cout << "Enter the number of moon pies stolen each day:";
cout << void enterStolenMoonPies(int* array, int numDays) << endl;
cout << "---------Results--------------"<< endl;
cout << "Total number of Moon Pies stolen: ";
int* totalMoonPies(int* array, int numDays);
cout << "Average number of Moon Pies stolen per day: ";
float averageMoonPies(int numDays, int sum);
cout << "Highest amount of Moon Pies stolen in a day: ";
int* highestMoonPies(int* array, int numDays);
cout << "Lowest amount of Moon Pies stolen in a day: ";
int* lowestMoonPies(int* array, int numDays);
}
#ifndef Moonpie_h
#define Moonpie_h
int* makeArray(int days);
void enterStolenMoonPies(int* array, int numDays);
int* totalMoonPies(int* array, int numDays);
float averageMoonPies(int numDays, int sum);
int* highestMoonPies(int* array, int numDays);
int* lowestMoonPies(int* array, int numDays);
#endif /* Moonpie_h */
#include <iostream>
//#include "Moonpie.cpp"
using namespace std;
int* makeArray(int days)
{
int* numDays = new int[days];
return numDays;
}
void enterStolenMoonPies(int* array, int numDays)
{
int stolenMP = 0;
for(int i =0; i<numDays; i++)
{
cout << "Enter the number of moon pies stolen each day:" << endl;
cout << "Day " << i << ": ";
cin >> stolenMP;
}
}
int* totalMoonPies(int* array, int numDays)
{
int* sum = 0;
for(int i = 0; i < numDays; i++)
{
sum += array[i];
}
return sum;
}
float averageMoonPies(int numDays, int sum)
{
float average;
average = static_cast<float>(sum)/numDays;
return average;
}
int* highestMoonPies(int* array, int numDays)
{
int* highest;
highest = &array[0];
for(int i = 0; i <= numDays; i++)
if (&array[i] > highest)
highest = &array[i];
return highest;
}
int* lowestMoonPies(int* array, int numDays)
{
int* lowest;
lowest = &array[0];
for(int i = 0; i <= numDays; i++)
if (&array[i] < lowest)
lowest = &array[i];
return lowest;
}
答案 0 :(得分:0)
就像在注释中一样,您的代码在许多方面都不正确。 您的函数不应被声明两次(标头中有一个,而源代码中有一个),但这只是众多问题中的一个。
cout << "Enter the number of moon pies stolen each day:";
cout << void enterStolenMoonPies(int* array, int numDays) << endl;
不起作用。 至少应该是
cout << "Enter the number of moon pies stolen each day:";
cout << enterStolenMoonPies(array, numDays) << endl;
删除所有main函数声明,并包括相应的头文件。
您最好从一个简单的程序重新开始,并考虑将这些指针用于开始,因为它比较复杂。