Weird compiler error when passing 2D array to function

I'm getting an odd compiler error that I can't make heads or tails of. I've narrowed down that it's related to me passing a 2D array as a function argument. The error specifically points out the lines that the function calls are on as well as the argument in each, both of which are me passing the array "wordsIntersect" as an argument, and both function calls are the only points in my program where I pass that array as an argument. My question is why is this generating an error, and what is the error related to? I'm following every example I can find of how to pass a 2D array by reference pretty much verbatim, I can't figure it out. Help appreciated, I've been at this for hours!

NOTE: Not all of my program is here, I've removed function definitions, declarations, etc. that aren't relevant to the issue.

The error is as follows (typed verbatim):
CrosswordGenerator_test.cpp: In function ‘int main()’:
CrosswordGenerator_test.cpp:72:53: error: cannot convert ‘std::string (*)[(long
int)word_count] {aka std::basic_string<char> (*)[(long int)word_count]}’ to ‘std
::string (*)[643] {aka std::basic_string<char> (*)[643]}’ for argument ‘2’ to ‘v
oid compare_words(std::string*, std::string (*)[643], int)’
CrosswordGenerator_test.cpp:97:38: error: cannot convert ‘std::string (*)[(long
int)word_count] {aka std::basic_string<char> (*)[(long int)word_count]}’ to ‘con
st string (*)[643] {aka const std::basic_string<char> (*)[643]}’ for argument ‘1
’ to ‘void write_csv(const string (*)[643], int)’

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
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//Function declarations
void compare_words(string arr1d[], string arr2d[][643], int dimension);
void write_csv(const string arr[][643], int dimension);

int main()
{
	//Determine number of words in txt file
	int word_count = count_words_txt(infile_name);

	//Create an array to hold the words in the txt file and read file values into the array
	string wordArray[word_count];		//Holds processed strings from text file, with non-alphabetical characters removed
	value_array(wordArray, word_count);

	//Remove non-alphabetical characters within array
	remove_non_alpha(wordArray, word_count);


	//Sort array in descending order by length
	selection_sort_by_length_desc(wordArray, word_count);


	//Define wordsIntersect array to hold values of overlapping characters for words in wordArray,
		//and value array with shared letters between words in wordArray
	string wordsIntersect[word_count][word_count];
	compare_words(wordArray, wordsIntersect, word_count);  //THIS IS THE ERROR LINE 72

	//Write wordsIntersect to .csv file
	write_csv(wordsIntersect, word_count);  //THIS IS THE SECOND ERROR LINE 97

	return 0;
}

//Function definition for compare_words
void compare_words(string arr1d[], string arr2d[][643], int dimension)
{
	//Outer anchor loop
	for (int a = 0; a < dimension; a++)
	{
		//Inner scan loop
		for (int s = 0; s < dimension; s++)
		{
			//If the anchor and scan elements are the same, you are comparing a word to itself, so set to a blank string
			if (a == s)
				arr2d[a][s] = "";
			//Otherwise, return the letters in common between both strings and assign the value to the 2d array
			else
				arr2d[a][s] = sharedLetters(arr1d[a], arr1d[s]);

			//////TEST STUB
			cout << "Comparing " << arr1d[a] << " against " << arr1d[s] << ". ";
			cout << "Resulting string is " << arr2d[a][s] << endl;
		}
	}
}

//Function definition for write_csv
void write_csv(const string arr[][643], int dimension)
{
	//Open file
	ofstream outfile(outfile_name);

	//Test file open
	if (outfile)
	{
		//Outer loop for row number
		for (int r = 0; r < dimension; r++)
		{
			//Inner loop for column number
			for (int c = 0; c < dimension; c++)
			{
				outfile << r << "," << c << "," << arr[r][c] << "\n";
			}
		}
		//Close file
		outfile.close();
	}
	//If file did not open, display error mesage and exit program
	else
	{
		cout << "Error, the output file did not open successfully.  Press enter to exit" << endl;
		cin.get();	//Prompt user to press enter to proceed
		exit(0);	//End program with successful exit value
	}

	////TEST STUB
	cout << "The write file function has completed" << endl;
}
Last edited on
NOTE: Not all of my program is here, I've removed function definitions, declarations, etc. that aren't relevant to the issue.


Please post something we can compile. I compiled in cpp.sh but missing code causes lots of errors.

1
2
3
4
5
6
7
int main()
{
	//Determine number of words in txt file
	int word_count = count_words_txt(infile_name);

	//Create an array to hold the words in the txt file and read file values into the array
	string wordArray[word_count];	


Does that work? The size of an array has to be a compile time constant. It's a lot easier to use a std::vector you won't have to fool around with subscripts so much - or wonder why they are not the same.

It all looks overcomplicated to write a csv file. Why have you got a 2d array of strings - is that supposed to model lines made up of words? What is the purpose of the assignment? It's a good idea to post that verbatim - we might have different ideas to you on the solution.

Edit: Also post an example of your input file

Last edited on
Topic archived. No new replies allowed.