C++ C+中的指针和引用有问题+;
我试图调用一个函数,该函数将更改对变量的引用,但每次运行该程序时,我都会得到一个错误的访问C++ C+中的指针和引用有问题+;,c++,pointers,C++,Pointers,我试图调用一个函数,该函数将更改对变量的引用,但每次运行该程序时,我都会得到一个错误的访问 #include <iostream> #include <fstream> #include "calculations.hpp" using namespace std; int main(int argc, const char * argv[]) { char input[12]; string input2; string nameOfFood
#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;
#包括
#包括
#包括“计算.hpp”
使用名称空间std;
int main(int argc,const char*argv[]{
字符输入[12];
字符串输入2;
食物的名称;
int*numberOfServings=0;//这就是我不断获得错误访问的地方
int*卡路里=0;
浮点数*fatInGrams=0;
浮球*carbsInGrams=0;
浮点*光纤图=0;
浮点数*保护图=0;
整数计数=0;
//打开输出文件
ofstream outData;//忽略
//开始循环
对于(int i=0;i*numberOfServings)
>>*卡路里
>>*法丁格拉姆斯
>>*卡宾卡
>>*纤维图像
>>*蛋白质图谱;
计算(食物名称、份数、卡路里、脂肪、碳水化合物、纤维、蛋白质);
i=0;
计数++;
}
如果(输入[0]=“n”){
i=5;
}
}
cout您应该传递有效变量的地址以进行计算
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.
初始化变量时,如:
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;
停止使用指针。从代码中删除所有指针。好了,就是这样。:)好的,那么当我在计算函数中时,它是如何工作的?
cin ....
>> numberOfServings // Drop the *
int *calories = 0;
float *fatInGrams = 0;
float *carbsInGrams = 0;
float *fiberInGrams = 0;
float *proteinInGrams = 0;