在C++中使用指针和引用时遇到问题
Having trouble with pointers and references in C++
>我正在尝试调用一个函数来更改对变量的引用,但每次运行程序时我都会获得错误的访问。
#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.
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 编译包含字符串的代码时遇到问题
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 遇到新行时,有没有办法停止istream_iterator
- 布尔值是通过引用传递的,但我仍然在 cpp 中遇到编译错误
- 使用引用和取消引用运算符读取代码行时遇到问题
- 我正在处理一个 c++ 项目,但在通过引用传递 rect0 对象时遇到问题
- 在C++中使用指针和引用时遇到问题
- shared_ptrs是否会遇到由于引用计数器原子递增/递减而导致的缓存丢失?
- 我在使用ifndef时遇到问题.我得到错误:错误未定义的引用