Passing structure array to a function?

I held off asking because it feels like something so fundamental that I should be able to find resources on google or youtube or any of the dozens of c++ tutorial sites out there, but no matter how many hours I spend with these, it's just not clicking.

Basically, I'm using header files to define two distinct structures and then calling an array of each type from the .cpp file. My compiler is giving me the expected grocery list of errors (but unlike usual compiler errors that give hints as to how to fix them, these are so obfuscated that they might as well be in Doth'raki) and I honestly don't know enough to even make the first step in correcting. I don't expect anyone to respond to this "I don't know what I'm doing, please help!" type post, and I apologize in advance for any forum clutter this might cause, but I'd be endlessly grateful for any help I might receive.

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
//main.cpp
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include "salestype.h"
#include "productlog.h"
using namespace std;

bool readfile(ifstream & salesf, ofstream & outfile, salesType & salesdata[]);

int main() {
	salesType salesdata[30];
	productType productdata[50];
	ifstream salesf, productf;
	ofstream outfile;

	readfile(salesf, outfile, salesdata[30]);
}
bool readsales(ifstream & salesf, ofstream & outfile, salesType & salesdata[]) {
    salesf.open("salesFile.txt");
    outfile.open("Outputfile.txt");
	if (salesf.is_open()) {
		return true;
	}
	else {
		cout << "Could not open file.  Please exit program.\n";
		return false;
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//salestype.h
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct salesType
{
	string	firstname;
	string	lastname;
	int	id;
	float	numsales;
	float	bonus;
  
};

1
2
3
4
5
6
7
8
9
10
11
12
//productlog.h
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct productType
{
	string	productnum;
	float	price;
};
Last edited on
Hi,
So they are linker errors. Can you post full error logs from your compiler here please?
Would be helpful if you could post the error messages and also the readfile function.
In an effort of brevity I only copypasta'd the "red" text in the build log, as the compiler log is huge for this code. If this means I left out something important, I can copy the entire brick of text instead.
1
2
3
4
5
6
7
8
9
10
11
E:\CSC201\Programming Assignment Four\Assignment Four.cpp:18:76: error: declaration of 'salesdata' as array of references
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/bits/ios_base.h:788:5: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/bits/basic_ios.h:64:11: error: within this context
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/streambuf:800:7: error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]' is private
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/fstream:69:11: error: within this context
E:\CSC201\Programming Assignment Four\Assignment Four.cpp:26:41: error: cannot pass objects of non-trivially-copyable type 'std::ifstream {aka class std::basic_ifstream<char>}' through '...'
E:\CSC201\Programming Assignment Four\Assignment Four.cpp:26:41: error: cannot pass objects of non-trivially-copyable type 'std::ofstream {aka class std::basic_ofstream<char>}' through '...'
E:\CSC201\Programming Assignment Four\Assignment Four.cpp:26:41: error: cannot pass objects of non-trivially-copyable type 'struct salesType' through '...'
E:\CSC201\Programming Assignment Four\Assignment Four.cpp:29:77: error: declaration of 'salesdata' as array of references
E:\CSC201\Programming Assignment Four\Assignment Four.cpp:30:5: error: 'salesf' was not declared in this scope
E:\CSC201\Programming Assignment Four\Assignment Four.cpp:31:2: error: 'outfile' was not declared in this scope

also worth noting that what I titled as "main.cpp" here is "Assignment Four.cpp" to my compiler


and also the readfile function.


That was one of the errors I just never caught, thank you. Meant to be the "readsales()" at the bottom.
Last edited on
Line 10: You can't pass an array by reference.

Line 18: This is not the correct way to pass an array. You're trying to pass the 31st element of the array (which is out of bounds). You don't check the result of the function call.

Line 19: Missing return 0;

Line 20: Your function name does not match. Your forward and function call are to readfile.
Reference to salesdata needs to be removed. readfile is really a misnomer since all you are doing is opening the files. You only check that the open of one file is successful.

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
//main.cpp
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include "salestype.h"
#include "productlog.h"
using namespace std;

bool openfiles(ifstream & salesf, ofstream & outfile, salesType salesdata[]);  // removed ref to salesdata

int main() 
{   salesType salesdata[30];
	productType productdata[50];
	ifstream salesf, productf;
	ofstream outfile;

	if (! openfiles(salesf, outfile, salesdata))  // Removed [30]
	    return 1;   // open failed.  Message already displayed.
    //  Do some work	    
    return 0;   //  program successful	    
}
bool openfiles (ifstream & salesf, ofstream & outfile, salesType salesdata[]) 
{   salesf.open("salesFile.txt");
    outfile.open("Outputfile.txt");
	if (! salesf.is_open()) 
	{   cout << "Unable to open salesfile.txt" << endl;
	    return false;
    }
    if (! outfile.is_open ())
    {   cout << "Unable to open outputfile.txt" << endl;
        return false;
    }
    return true;	    // file open successfully
}







Last edited on
Thank you! Seeing that made that much "click" in as far as I can recreate that pretty consistently now on intuition, even if I'm not quite sure why the rules regarding array brackets feel so arbitrary. If I'm trying to call variables from the structured header file, how do I cite the structure?

i.e. Assuming the same header files in my OP, am I supposed to be doing this loop without array brackets (still doesn't work that way) or do I have to come up with a completely different salesType variable to call salesType variables?
1
2
3
4
5
6
7
8
9
10
void calcsales(ifstream & salesf, ofstream & outfile, salesType salesdata[], productType productdata[]) {
	do {
			salesf 	>> 	salesdata[].firstname >> salesdata[].lastname >> salesdata[].id; 
//"firstname", "lastname", and "id" are all variables stored in the structure header file that salesdata[] is named for
		while (productdata[].productnum!=-1 || salesdata[].numsales!=-1) {
			salesf	>>	productdata[].productnum >> salesdata[].numsales;
		}
//"numsales" is a salesdata[] structured variable and "productnum" is a productdata[] structured variable
	} while (salesf);
}


And what if I want to cout that data? Do I still use type.variable syntax or do other rules apply?

Lastly, it compiles if I replace the dots with "->" and removes the array brackets, but I honestly don't know if it's referencing the data the same way with that method or not. It's not outputting anything at all when I run it with test cout statements. Also seems to undermine the entire array when I can't call it at all without losing the brackets, and by extension, the "array" part of the array.

And how do I call arrays by reference? Everything I've tried doesn't work but nothing I've found has said that it can't be done at all.
Last edited on
Topic archived. No new replies allowed.