【发布时间】:2011-04-24 20:38:16
【问题描述】:
在学习了 2 年的 java 之后,我大约 3 周前开始学习 c++。看起来很不一样,但我到了那里。我的讲师是一个可爱的人,但任何时候我问一个问题,为什么事情会这样或那样。他只是回答“因为它是”。
下面的代码中有很多 cmets 带有一些随机问题,但主要问题是我遇到了两个构建错误,一个说 arraytotal 尚未初始化(即使我找到了它的值),另一个说main 中的外部引用。
有没有人介意阅读代码并回答其中的一些问题,也许是我遇到的整体问题?
#include<string>
#include<fstream>
#include<ostream>
using namespace std;
//double decimals[5] ={2,4,6,8,10};
const int arraySize = 5;
// does an arraySize have to be const always? is it so it doesnt channge after the array has been created?
//double decimals[arraySize];
/*
this array is being created in the function averageN() but why?
cant i just create it up top and reference it in?
*/
// why do you have to write the name of the function up here before you even create it?
double averageN();
int main()
{
averageN();
return 0;
}
// why does the array have to be created here?
double averageN(double decimals[arraySize])
{
double average;
double arrayTotal;
for (int i = 0; i<5;i++)
{
// fills with random numbers from 0 - 10
decimals[i] = (0+(rand()%10));
}
// find the total of all the elements in the array
for (int i = 0; i < arraySize;i++)
{
double currentElement = decimals[i];
arrayTotal = (currentElement+arrayTotal);
//arrayTotal +=decimals[i]) ;
}
// return the average
average = (arrayTotal/arraySize);
return 0.0;
}
【问题讨论】:
-
该代码能编译吗?它似乎没有。最好先获得一段可编译的代码以更好地理解
-
请再次检查您的帖子。我认为您需要格式化文本。我可以看到#include #include #include。缺少头文件名。
-
这是问题的一部分。我不知道为什么它不会构建。
-
@SB,代码无法编译,OP 正是在寻求帮助以使其首先编译。
-
抱歉没听清楚。
标签: c++ arrays initialization unresolved-external