I'm having trouble with getting my syntax correct while trying to call a function, where one of the parameters for the function is an array of structs i'm trying to pass in by reference.
The code compiled fine after I finished writing the function, and my struct works fine, so I think it's a problem with how I'm calling it, but I haven't been able to figure out what I'm doing wrong.
I'm compiling on a Win7 PC, using g++ in Cygwin. The error I'm getting is as follows:
$ g++ a07.cpp
a07.cpp: In function `int main()':
a07.cpp:99: error: invalid initialization of non-const reference of type 'albumDatabase&' from a temporary of type 'albumDatabase (*)[100]'
a07.cpp:23: error: in passing argument 1 of `void listSort(albumDatabase&, int, char&)'
a07.cpp:104: error: invalid initialization of non-const reference of type 'albumDatabase&' from a temporary of type 'albumDatabase (*)[100]'
a07.cpp:23: error: in passing argument 1 of `void listSort(albumDatabase&, int, char&)'
#define listSizeMax 100
struct albumDatabase {
string title;
string band;
} listCD[listSizeMax];
void listSort(albumDatabase&,int,char&); // This is line 23 in my code
int main() {
int i, listSize;
string fileCD, searchToken;
char selection,sortPrimary;
do
{
cout << "Sort by [T]itle, or by [B]and name?: ";
cin >> sortPrimary;
cin.ignore(1000,'\n');
switch(sortPrimary)
{
case't':
case'T':
listSort(&listCD,listSize,'t'); // This is line 99 in my code
cout << "Sorting database by song title...";
break;
case'b':
case'B':
listSort(&listCD,listSize,'b'); // This is line 104 in my code
cout << "Sorting database by band name...";
break;
case'x':
break;
default:
cin.clear();
cout << "Invalid input. Please try again. ";
break;
}
}while(sortPrimary != 'x');
}
void listSort(albumDatabase (&listCD)[listSizeMax], int length, char& sortPrimary)
{
// Stuff happens here that takes up a lot of whitespace.
// I don't believe it's relevant to this issue.
}
I feel like the answer is probably something trivial, but I don't understand the documentation well enough to read it clearly, and I don't see anything as obvious as a missing bracket or a misplaced semicolon. Thank you in advance for your time!
It is always always wise to pass the size of the array along with the array as a separate parameter, because usually when you pass a array to a function you are going to be accessing it somehow so you don't want to go out of bound. Though it looks like you might be doing this but it is hard to tell.
Though thankfully C++11 gave us something we can do instead and now we no longer have to pass the size of a array as a argument anymore :).
We now can use use std::begin() and std::end() which will return a iterator to the beginning of the array or a iterator to 1 past the end of the array.
So we could do something like this.
1 2 3 4 5
void printData(albumDatabase myDatabase[])
{
for (auto it = begin(myDatabase); it != end(myDatabase); ++it)
cout << *it << endl;
}
Or if you wish to utilize even more C++11 features something like this
1 2 3 4 5
void printTitles(albumDatabase myDatabase[])
{
for (auto album : myDatabase)
cout << album.title << endl;
}
Once I implemented your advice and then figured out that I was also trying to pass a literal by reference in the third argument, I managed to get this sucker to compile :D