Hi, very new to C++ so sorry for what is probably a very fundamental question however no amount of reading and searching seems to be able to give me an understanding of how references and pointers should be used.
The below program compiles with the error and note
1 2 3 4 5 6 7
CostCalculator.cpp:75:2: error: no matching function for call to 'print_expense'
print_expense(data);
^~~~~~~~~~~~~
CostCalculator.cpp:53:6: note: candidate function not viable: no known
conversion from 'expense_data' to 'expense_data *'for 1st argument; take
the address of the argument with &
void print_expense(expense_data *data)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "terminal_user_input.h"
usingnamespace std;
enum expense_kind {Fixed, Flexi, Disc};
typedefstruct expense_data
{
my_string name;
int expense_id;
expense_kind kind;
int cost;
} expense;
typedefstruct expense_array
{
int size;
expense *data;
} expense_array;
expense_kind read_expense_kind()
{
int input;
std::cout << "1: Fixed \n";
std::cout << "2: Flexible \n";
std::cout << "3: Discretionary \n";
input = read_integer_range ("Please choose expense kind: ", 1, 3);
return expense_kind(input - 1);
}
expense_data read_expense()
{
expense_data result;
printf("Please enter expense data\n");
result.name = read_string("Please enter name: ");
result.expense_id = read_integer("Please enter expense ID: ");
result.kind = read_expense_kind();
result.cost = read_integer("Please enter cost: ");
do {
printf("Please enter a cost greater then 0\n");
result.cost = read_integer("");
} while(result.cost < 0);
return result;
}
void print_expense(expense_data *data)
{
printf("%s (%d), ", data->name.str, data->expense_id);
switch(data->kind) {
case Fixed: printf("fixed, ");
break;
case Flexi: printf("flexible, ");
break;
case Disc: printf("discretionary, ");
break;
}
printf("$%d ", data->cost);
if (data->cost > 5000)
printf("requires justification");
elseif (data->cost >10000)
printf("requires authorisation");
}
int main()
{
expense_data data;
data = read_expense();
print_expense(data);
return 0;
}
My understanding was that read_expense() can use references as it does not have a variable input but print_expense() must use pointers because its input can vary? Is there a problem with the data variable that has been setup in main()?
As you can probably tell I am very confused and any help getting my head around this would be greatly appreciated.
print_expense expects a pointer. You are not passing a pointer you are passing the data structure itself.
You need to use the address of operator(&) to get a pointer to the data structure.
print_expense(&data);
The use of pointers at all is frowned upon these days. We favor smart pointers instead.
references and pointers are basically interchangeable. Pointers are necessary when using polymorphism or dynamically allocated memory, however. I would favor references whenever possible, they are less error prone.
references and pointers are basically interchangeable. Pointers are necessary when using polymorphism or dynamically allocated memory, however
References are behaving dynamically (it is a common pattern to have a reference to base as function parameter) and you can have a reference to dynamic memory.