2016-03-22 5 views
-2

変数への参照を変更する関数を呼び出そうとしていますが、プログラムを実行するたびに不正なアクセスが続いています。C++でポインターと参照に問題がある

私はこれを理解できないようです
#include "calculations.hpp" 
#include <iostream> 
using namespace std; 

void calculate(string nameOfFood, int *numberOfServings, int *calories, float *fatInGrams, float *carbsInGrams, float *fiberInGrams, float *proteinInGrams){ 
    *numberOfServings = 24; 
    *calories = 15; 
} 

、私はそれができる知っている

#ifndef calculations_hpp 
#define calculations_hpp 

#include <stdio.h> 
#include <iostream> 
using namespace std; 

void calculate(string nameOfFood, int *numberOfServings, int *calories, float *fatInGrams, float *carbsInGrams, float *fiberInGrams, float *proteinInGrams); 

#endif /* calculations_hpp */ 

と計算関数ファイル:

#include <iostream> 
#include <fstream> 
#include "calculations.hpp" 

using namespace std; 

int main(int argc, const char * argv[]) { 
    char input[12]; 
    string input2; 
    string nameOfFood; 
    int *numberOfServings = 0;// THIS IS WHERE I KEEP GETTING THE BAD ACCESS 
    int *calories = 0; 
    float *fatInGrams = 0; 
    float *carbsInGrams = 0; 
    float *fiberInGrams = 0; 
    float *proteinInGrams = 0; 
    int count = 0; 
// Open an output file 
    ofstream outData; //IGNORE 
// Start a loop 

    for(int i = 0; i<5; i++){ 
     // On the console, ask if the user ate. If the answer is No, be creative, but polite in your answer. 
     cout << "did you eat? yes or no" << endl; 
     cin >> input; 
     if (input[0] == 'y'){ 
      cout << "Please enter the name; number of servings; calories; amount of fat"; 
      cout << "; number of carbs; amount of fiber; and the amount of protein" << endl; 
      cin >> nameOfFood 
      >> *numberOfServings 
      >> *calories 
      >> *fatInGrams 
      >> *carbsInGrams 
      >> *fiberInGrams 
      >> *proteinInGrams; 
      calculate(nameOfFood, numberOfServings, calories, fatInGrams,carbsInGrams, fiberInGrams, proteinInGrams); 
      i = 0; 
      count++; 

     } 
     if (input[0] == 'n'){ 
      i = 5; 
     } 
    } 
    cout << count << endl; 
    cout << "calories is " << &calories << endl; 
    cout << "number of servings is " << &numberOfServings << endl; 

計算機能はのヘッダファイルを持っています私がObjective-Cのクラスでこれまでに見たことがあれば、どんな助けもありがたいです。ありがとうございました。

+4

に類似の修正が単にポインタの使用を停止してください。コードからすべてのポインタを削除します。そこは、それだけです。 :) –

答えて

0

計算するために有効な変数のアドレスを渡すことになっています。

string nameOfFood; 
int numberOfServings = 0; 
int calories = 0; 
float fatInGrams = 0; 
float carbsInGrams = 0; 
float fiberInGrams = 0; 
float proteinInGrams = 0; 

calculate(nameOfFood, &numberOfServings, &calories, &fatInGrams, &carbsInGrams, &fiberInGrams, &proteinInGrams); 
         ^Address of numberOfServings 

calcualte関数にあります。

calculate(string nameOfFood, int* numberOfServings, int* calories, float* fatInGrams, float* carbsInGrams, float* fiberInGrams, float* proteinInGrams) 
{ 
    // Inside the function. 
    // numberOfServings is a pointer to the variable. 

    // Thus *numberOfServings dereference the pointer and 
    // allows you to assign the variable that it points at. 

    *numberOfServings = 24; // modifies the variable that was pointed at. 
+0

わかりました。次に、私が計算機能に入っているとき、それはどのように機能しますか? – cflies1121

+0

ありがとうございます。 – cflies1121

2

あなたのような変数の初期化:

int *numberOfServings = 0; 

それが何を指しています。このようなポインタを参照解除すると、未定義の動作になります。したがって、行:

cin .... 
     >> *numberOfServings 

が間違っています。

最も単純な修正は、変数numberOfServingsをタイプintのオブジェクトとして宣言することです。ポインタではありません。その後、

int numberOfServings = 0; 

と、それは次のように読み込まれる場所を変更します。

cin .... 
     >> numberOfServings // Drop the * 

int *calories = 0; 
float *fatInGrams = 0; 
float *carbsInGrams = 0; 
float *fiberInGrams = 0; 
float *proteinInGrams = 0; 
関連する問題