Error when returning structure in function?

I am accepting a structure array as an argument, and trying to return that structure array.

I am getting this error:
"no suitable constructor exists to convert from "Cost" to "Cost".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "stdafx.h"
#include<iostream>
#include<string>
#include <iomanip>
#include "cost.h"
#include <fstream>
using namespace std;

Cost parseAccount(fstream dataFile, Cost cost[4]);

int main()
{
	fstream dataFile;
	Cost cost[4];
	const int SIZE = 20;
	string input;

	dataFile.open("text.txt", ios::in);

	getline(dataFile, input);
	cout << input << endl;

	for (int i = 0; i < 4; i++)
	{
		cout << left << setw(15) << cost[i].description << "\t";
		cout << cost[i].amount << "\t";
		cout << cost[i].itemNum << endl;
	}
    return 0;
}

Cost parseAccount(fstream dataFile, Cost cost[])
{

	for (int i = 0; i < 4; i++)
	{
		dataFile >> cost[i].description;
		dataFile >> cost[i].amount;
		dataFile >> cost[i].itemNum;
	}
	return cost;
}

I am accepting a structure array as an argument, and trying to return that structure array.

But your function signature says that you're trying to return a single Cost.

Why are you trying to return the Cost array? You've passed a pointer to that array as a parameter so any changes you make to that array in the function are already reflected in the array in the calling function.

So you're saying that I don't need to return anything then? Since the chances will already reflect in the calling function?
Correct, the changes you make in the function will be reflected in the calling function.

Even though Cost cost[] looks like you're passing an array, in reality that's just a pointer Cost *cost. When you're passing the cost array to the function, you're just passing the pointer to the first Cost in the array. So any modifications you do in the function will be reflected in the array.

This also means that there is no point in specifying the size of the array when specifying the parameter type, as you do here: Cost parseAccount(fstream dataFile, Cost cost[4]);. Also, you probably want to pass the fstream object by reference.

Finally, you seem to have forgotten to actually call parseAccount in main before printing the values of the cost array.
Example with helper methods for parsing and outputting Costs to streams. I set the parse method itself to accept istream reference, so it should support istringstream, ifstream, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

struct Cost
{
    Cost()
    {
    }
    
    // Useful, but unused in this example
    Cost(string description, int amount, int itemNum) :
        description(description),
        amount(amount),
        itemNum(itemNum)
    {
    }
    
    string description;
    int amount;
    int itemNum;
};

// Helper to parse input streams into Costs
istream& operator>>(istream& is, Cost& c)
{
    is >> c.description >> c.amount >> c.itemNum;
    return is;
}

// Helper to send Costs to an output stream
ostream& operator<<(ostream& os, const Cost& c)
{
    os << setw(20) << c.description << 
          setw(5) << c.amount << 
          setw(12) << c.itemNum << endl;
    return os;
}

// Parses Costs from a stream into a vector
vector<Cost> ParseCosts(istream& is)
{
    vector<Cost> v;
    Cost c;
    while (is >> c)
    {
        v.emplace_back(c);
    }
    return v;
}


int main() 
{
    const char* data_text = R"LITERAL(Hat 5 10000037
Baseball 13 10000142
Tennis_Racket 7 10000023
Hockey_Stick 3 10001111
Bowling_Ball 1 10000699
Cleats 16 10002345
)LITERAL";

    // Imitate file stream
    istringstream iss(data_text);
    
    // Header
    cout << setw(20) << "Description" << 
          setw(5) << "Amt" << 
          setw(12) << "Item#" << endl << endl;
          
    auto costs = ParseCosts(iss);
    for (auto& c : costs)
        cout << c;
    cout << endl;
    
    return 0;
}


can test at https://repl.it/repls/RespectfulLivelyOctagon
         Description  Amt       Item#

                 Hat    5    10000037
            Baseball   13    10000142
       Tennis_Racket    7    10000023
        Hockey_Stick    3    10001111
        Bowling_Ball    1    10000699
              Cleats   16    10002345
Topic archived. No new replies allowed.