在C++中使用指针和引用时遇到问题

Having trouble with pointers and references in C++

本文关键字:引用 遇到 问题 指针 C++      更新时间:2023-10-16

>我正在尝试调用一个函数来更改对变量的引用,但每次运行程序时我都会获得错误的访问。

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

计算函数的头文件为 :

#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 大学课堂上看到的那样,任何帮助都将不胜感激。谢谢。

当您初始化变量时,例如:

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;

你应该传递一个有效变量的地址进行计算。

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.