我试图调用一个函数来改变对变量的引用,但每次运行程序时我都会遇到错误的访问。
#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;
calculate函数有一个头文件:
#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 "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;
}
我似乎无法解决这个问题,我知道它可以像我之前在Objective-C大学课程中看到的那样完成,任何帮助都会受到赞赏。谢谢。
答案 0 :(得分: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;
答案 1 :(得分: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
在计算功能中。
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.