0

ポインターと参照を扱ういくつかの新しい領域で、ポインターを使用して参照によって配列を関数に渡そうとしていますが、何を試してもエラーが発生し続けます。問題は非常に簡単に修正できると確信していますが、私の頭を包み込むようには見えませんが、誰かが私が犯している間違いを見ることができますか? どんな助けでも大いに役立ちます ありがとう

#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;              
}
4

2 に答える 2

5

C++ では、配列のサイズはその型にエンコードされます。

一般的な「double の配列」タイプはありません。しかし、「7 個の double の配列」タイプと「13 個の double の配列」タイプなどがあります。

そのため、配列を単なるポインターとしてではなく配列として関数に渡すには、関数のシグネチャで正確な型をエンコードする必要があります。

「配列を取る関数」ではなく、「サイズ7の配列を取る関数」になります。

その方法は次のとおりです。

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

もちろん、配列のサイズが固定されていない場合は、テンプレート化することもできます。

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

しかし実際には、あなたがやろうとしていることは、生の配列を使用して行うべきではありません。

標準ライブラリ ベクターを使用します。

于 2013-03-24T17:57:47.587 に答える
0

簡単に言えば、行を置き換える

arrayPointer = &temperaturesArray;

arrayPointer = temperaturesArray;

コードをコンパイルします。

arrayPointerは型double*temperaturesArrayあり、型double[MAX_COUNTER](with ) であることに注意してくださいMAX_COUNTER = 7。したがって、 a のアドレスに割り当てることarrayPointerはできますが、 a のアドレスにdouble割り当てることはできません。それが元のコードがやろうとしたことであり、コンパイルに失敗しました。arrayPointerdouble[MAX_COUNTER]

一方、 a の各要素は adouble[MAX_COUNTER]ですdouble。特に、最初の要素は adoubleであり、そのアドレスを に割り当てることができますarrayPointer:

arrayPointer = &temperaturesArray[0];

上記の修正は、この行のシンタティック シュガーにすぎません。実際、「 T 型の配列」型のオブジェクト(たとえばdouble[MAX_COUNTER]) を「T 型のポインター」に割り当てると、コンパイラーはいわゆる配列からポインターへの変換を実行します。これは、最初のアドレスを割り当てることを意味します。ポインタへの配列要素。

ここで、コード (提供された修正を含む) について少しコメントします。具体的には、次の行です。

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

上記の最初の行MAX_COUNTERは、タイプ のオブジェクトの配列を格納するためにヒープ メモリを割り当てますdouble。次に、この配列の最初の要素のアドレスが に割り当てられarrayPointerます。

次に、次の行arrayPointerは の最初の要素のアドレスに再割り当てしますtemperaturesArray。したがって、ヒープに割り当てられた配列の最初の要素のアドレスが失われ、それができなくなりdeleteます。へのすべての呼び出しは、へnewの呼び出しと一致する必要があることに注意してdeleteください (そうしないと、メモリ リークが発生します)。ただし、この特定のケースでは、呼び出すのが最善の方法ですdelete。実際にはnew、ヒープ メモリは使用されないため、 への呼び出しを削除する必要があります。より正確には、上記の最初の行を削除できます。

于 2013-03-24T18:04:05.390 に答える