2013-03-24 100 views
0

在一些使用指针和引用的新领域,我试图通过引用将数组传递给使用指针的函数,但是无论我尝试什么,我都会收到错误,我确定这个问题很简单,但我似乎无法绕过它,任何人都可以看到我犯的错误吗?任何帮助将会有很大的帮助在C++中使用指针传递数组通过引用

#include<iostream> 
#include<cmath> 
#include <iomanip> 
#include <cstdio> 
#include <cstdlib> 
#include <new> 

using namespace std; 

//Inline function 
inline double getFahrenheit(double theCelsius) 
{ 
//Convert the celcius to farenheit 
return (theCelsius + 32) * 5/9; 
} 

void outputWeather(double *temperaturesArray, const string WEEK_DAY_NAMES[], const  double MAX_NUMBER) 
{ 
    //this is a counter that will increment through the days and various 
    int counter; 
    //reset the counter to 0 so we can use it again  
    counter = 0; 
    //print a header  
    cout << "THIS WEEKS TEMPERATURE REPORT " << endl; 
    //print a divider 
    cout << "=============================" << endl; 
    //while the counter is less than 7 repeat again 
    while(counter < MAX_NUMBER) 
    { 
     //print out the temperatures by day   
     cout << WEEK_DAY_NAMES[counter] << "  " << temperaturesArray[counter] << "\370C " << getFahrenheit(temperaturesArray[counter]) <<"\370F "<< endl; 
     //increase the counter by 1 
     counter +=1; 
    } 
} 

//Function that will determine whether or not the value the user entered was numeric  and within the range 
double checkValidation(string weekDay) 
{ 
//Create a variable to store a valid number 
double validNumber; 

//This will hold the value for the lowest 
const double MIN_NUMBER = 1; 
//This will hold the value for the highest temperature 
const double MAX_NUMBER = 365; 
//This will hold the value for the valid number that the user will eventually enter 
validNumber = 0.0; 

//This will alert the user to enter a temperature for that day of the week 
cout << "Please enter the temperature for " << weekDay << endl; 
//This will take in teh value the user entered for teh temperature 
cin >> validNumber; 

    //If the text the user entered was not numeric start again    
if(cin.fail())    
{ 
    //C++ built in methods for clearing the cin      
    cin.clear();    
    fflush(stdin); 

    //alert the user what they typed was wrong 
    cout << "invalid input. please try again and enter a numeric value" << endl; 
    //pass in the weekeday and start over 
    checkValidation(weekDay);      
} 
else 
{ 
    //if teh number falls outside the range 
    if(validNumber < MIN_NUMBER || validNumber > MAX_NUMBER) 
    { 
     //Alert the user that it was outside the range   
     cout << "invalid input. please try again and enter a value between -90 and 60" << endl; 
     //pass in the weekday and try again   
     checkValidation(weekDay); 
    } 

} 
//return the valid number  
return validNumber; 

} 


int main() 
{ 
    //this is a counter that will increment through the days and various 
    int counter; 
    //a constant to hold the variable for the number of days 
    const int MAX_COUNTER = 7; 
    //an array that will hold all the days of the week 
    const string WEEK_DAY_NAMES[] = 
    { 
      "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 
    }; 

    //this will hold all of teh temperatures 
    double temperaturesArray[MAX_COUNTER]; 
    //start the counter off at 0  
    counter = 0; 

    //begin telling the user to enter temperatures by printing a header 
    cout << "Please enter the temperature for every day of the week " << endl; 
    //while the counter is less than 7 we will repeat 
    while(counter < MAX_COUNTER) 
    {              
    //add temperature to the array 
    temperaturesArray[counter] = checkValidation(WEEK_DAY_NAMES[counter]); 
    //add 1 to the counter    
    counter +=1;      

    } 

    double * arrayPointer = new double[MAX_COUNTER]; 

    arrayPointer = &temperaturesArray; 

    outputWeather(arrayPointer, WEEK_DAY_NAMES, MAX_COUNTER);  

system("PAUSE"); 
return 0;    
} 
+0

你得到的错误是什么? – imulsion 2013-03-24 17:51:43

+0

@imulsion当前代码告诉我 - 无法在作业中将“double(*)[7]”转换为“double *” – 2013-03-24 17:52:59

+0

您不能通过引用“通过传递指针”来传递任何东西。通过传递参考*来引用事物。如果你传递一个指针,那么你通过指针传递它们。 – jalf 2013-03-24 17:54:01

回答

5

在C++中,数组的大小被编码到它的类型中。

没有一般的“双打数组”类型。但是有一个“7双打”类型,和一个“13双打”类型,依此类推。

因此,要将数组作为数组传递给函数,而不是简单地将其作为指针,则需要在函数的签名中对精确类型进行编码。

它不会是“需要数组的函数”,而是“需要大小为7的数组的函数”。

做到这一点的方法如下:

void f(double (&arr)[7]); 

当然或者,你可以模板吧,如果数组大小不固定:

template <size_t N> 
void f(double (&arr)[N]); 

不过说真的,你”重新尝试做不应该使用原始数组完成。

使用标准库矢量。

0

简言之,更换线

arrayPointer = &temperaturesArray; 

arrayPointer = temperaturesArray; 

使得代码进行编译。

注意arrayPointerdouble*类型的,并且是temperaturesArraydouble[MAX_COUNTER]型()的。因此,您可以将arrayPointer分配给double的地址,但不能将arrayPointer分配给double[MAX_COUNTER]的地址。这就是原始代码试图做的事情,因此它未能编译。

另一方面,double[MAX_COUNTER]的每个元素是double。特别是,第一个元素是double,你可以将其地址分配给arrayPointer

arrayPointer = &temperaturesArray[0]; 

上面的修复仅仅是这条线synctatic糖。实际上,当分配型“阵列类型T的”的对象(例如double[MAX_COUNTER])为“指针类型T的”,则编译器执行所谓阵列到指针转换这意味着将第一个数组元素的地址赋给指针。

现在您的代码一点点的话(与所提供的固定),具体而言,下面的行:

double * arrayPointer = new double[MAX_COUNTER]; 
arrayPointer = temperaturesArray; 

上面的第一行分配的堆存储器来存储double类型的MAX_COUNTER对象的数组。然后该数组的第一个元素的地址被分配到arrayPointer

然后,下面的行将arrayPointer重新分配到第一个元素temperaturesArray的地址。因此,堆分配数组的第一个元素的地址会丢失,您不能再使用delete了。请记住,每次拨打new必须通过致电delete(否则您有内存泄漏)进行匹配。然而,在这种特殊情况下,最好的办法是不要致电delete。实际上,你应该消除对new的调用,因为从不使用堆内存。更准确地说,您可以删除上面的第一行。