1st Classes Program, Can't Call Function

This is my first attempt at a classes / objects program, and I'm just not getting it. The main problem seems to be that I cannot successfully call the function in testLocation.cpp

location.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef LOCATION_H
#define LOCATION_H

const int ROW_SIZE = 3; 
const int COLUMN_SIZE = 4; 

class Location
{ 
	public: 
		Location(); 
		Location(int newRow, int newColumn, double maxValue);  
		Location locateLargest(const double a[][COLUMN_SIZE]); 


	private: 
		int row, column; 
		double maxValue; 

}; 

#endif   


location.cpp
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
#include <iostream> 
#include "location.h"
using namespace std; 

Location::Location()
{
	row = 0; 
	column = 0; 
	maxValue = 0.0; 
} 

Location::Location(int newRow, int newColumn, double newMaxValue)  
{
	row = newRow; 
	column = newColumn; 
	maxValue = newMaxValue; 
}

Location::Location locateLargest(const double a[][COLUMN_SIZE]) 
{

	double temp = 0.0;
	int iTemp = 0, jTemp = 0; 

    for(int i = 0; i < ROW_SIZE; i++)
    {
	   	for (int j = 0; j < COLUMN_SIZE; j++) 
   		{ 
			if(a[i][j] > temp)
        	{	
				temp = a[i][j];
    			        iTemp = i; 
				jTemp = j; 
			}
		}
	
	cout << "The location of the largest element is " << temp << " at " << iTemp << ", " << jTemp << "." << endl;
    }
}


testLocation.cpp
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
#include <iostream>
#include "location.h" 
using namespace std; 

int main () 
{
	
	Location myLocation; 

	cout << "Enter a 3 by 4 two-dimensional array:" << endl; 
	double a [ROW_SIZE] [COLUMN_SIZE]; 
	
	for (int i = 0; i < ROW_SIZE; i++) 
  	{	
      for (int j = 0; j < COLUMN_SIZE; j++) 
      {
          cin >> a [i] [j]; 
      }
 	}         

 	// I need to call the locateLargest function. This seems right to me but won't compile, "no matching function" error:  
	// myLocation.locateLargest(a[ROW_SIZE][COLUMN_SIZE]);
	Location::Location locateLargest;  // This compiles, but at run time, this function call is ignored. 

	system ("PAUSE"); 
	return 0; 

} 


The program should find the largest number in the 2D array, and its location in the array. But the function call in testLocation.cpp never runs at run time.

1. How do I call this function?
2. Does the rest of my code appear correct for finding the largest number and its location?

Many Thanks
ROW_SIZE = ??
COLUMN_SIZE = ??
Location locateLargest(const double a[][COLUMN_SIZE]);
the type is wrong(that's is for constructor/destructor). Try void.
and to call it, just use:
myLocation.locateLargest(a);

and your done(I suppose..:))..
Okay, I changed "Location" to "void" in the location.h and location.cpp files. Unfortunately, it's still not working.

"myLocation.locateLargest(a);" is stopping the program from compiling. The error message is "[Linker error] undefined reference to `Location::locateLargest(double const (*) [4])'"

Suggestions are appreciated.
Last edited on

undefined reference to `Location::locateLargest(double const (*) [4])

it means that the definition of the function was not found or was not clearly stated.
I dunno how you changed those parts but it should be noted that they should be changed from:
location.h
Location locateLargest(const double a[][COLUMN_SIZE]);

to
void locateLargest(const double a[][COLUMN_SIZE]);

and from location.cpp
Location::Location locateLargest(const double a[][COLUMN_SIZE])

to
void Location::locateLargest(const double a[][COLUMN_SIZE])
//I'm betting you've got it wrong in this part :)

and btw, I think you should get this part out of the for loop. :)
cout << "The location of the largest element is " << temp << " at " << iTemp << ", " << jTemp << "." << endl;


----edited----
Location::Location locateLargest;

You dont use that in the main(That's a call for the function, but within the class scope only!!). This should be the one replaced with myLocation.locateLargest(a);
Last edited on
Okay, got it working now, and yes, you were right about where I had it wrong. Thank you very much.

Two questions:

1. I removed the overloaded constructor from location.h and location.cpp. The program works fine. Does this overloaded constructor serve a purpose that I'm not aware of, or is it unneeded in this case?

2. The instructions state "The return value is an instance of Location." Since I'm using a void function, I'm obviously not returning anything. How would I update the program to meet that requirement? What data type would I be returning? It seems that I have to return 3 things: the highest number (a double), the row (an int), and the column (an int).

Thanks again.
1) Overloaded constructors are used to have another way of calling the object's constructor, they are not really compulsory.
2) I'm sorry, I didn't know about that instruction. :) From what the instruction said,
is an instance of Location."
, so that means it is an object with a Location class you can change it back to what you just previously did(I changed a few things here btw):
Location locateLargest(const double a[][COLUMN_SIZE]); ///location.h
Location Location::locateLargest(const double a[][COLUMN_SIZE]) ///location.cpp
myLocation.locateLargest(a); ///main

I really think that void would be more appropriate here though , since you never use its return type anyway..:)

As for the other 3, You've already made them. But if you WANT to return them, as a value, make a function that returns them in the public scope. Say for example:
int myrow()
{return row;)
Note that it(row) is private.
Thank you for the update.

Location locateLargest(const double a[][COLUMN_SIZE]); ///location.h
Location Location::locateLargest(const double a[][COLUMN_SIZE]) ///location.cpp
myLocation.locateLargest(a); ///main

That's returning some weird results:

Enter a 3 by 4 two-dimensional array:
1 2 3 4
5 6 7 8
9 10 11 12
The location of the largest element is 2 at (0, 1072693248).
Press any key to continue . . .

So I'm not sure what went wrong there.

Also, I'm a little confused about that last part. How can I return the number, the row, and the column from the locateLargest function? Unless there's something I don't understand, I thought a function could return 1 thing. Here, it would be returning 3 things. Is there a way to do that?

Thanks again!
Last edited on
Did you delete something from your previous codes?(like initialization of int iTemp = 0;)
If the problem still persist, post the new code again(only the ones you changed).
Also what I meant is that you make other functions that return value, not locateLargest().
And yep, you're right, a function returns(to itself) one type.
If you want them in this one function, you can try templates. (see http://www.cplusplus.com/doc/tutorial/functions2/)
but that would make it more complicated, rather than making different functions as what I said above. Anyway, it's your call. :)
I think my main concern is why changing from void to Location resulted in erroneous results, so I'll try reposting my code. I'll pass on the templates, no need to make this more complicated. :)

location.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef LOCATION_H
#define LOCATION_H

const int ROW_SIZE = 3; 
const int COLUMN_SIZE = 4; 

class Location
{ 
	public: 
		Location(); 
		// Location(int newRow, int newColumn, double maxValue); 
		// An overloaded constructor does not appear to be needed for this program.  
		void locateLargest(const double a[][COLUMN_SIZE]); 

	private: 
		int row, column; 
		double maxValue; 

}; 

#endif   


location.cpp
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
#include <iostream> 
#include "location.h"
using namespace std; 

Location::Location()
{
	row = 0; 
	column = 0; 
	maxValue = 0.0; 
} 

// Overloaded constructor does not appear to be needed for this program. 
/*
Location::Location(int newRow, int newColumn, double newMaxValue)  
{
	row = newRow; 
	column = newColumn; 
	maxValue = newMaxValue; 
}
*/

void Location::locateLargest(const double a[][COLUMN_SIZE]) 
{

    for(int i = 0; i < ROW_SIZE; i++)
    {
	   	for (int j = 0; j < COLUMN_SIZE; j++) 
   		{ 
			if(a[i][j] > maxValue)
        	{	
				maxValue = a[i][j];
    			        row = i; 
				column = j; 
			}
		}
    }  
 	cout << "The location of the largest element is " << maxValue << " at (" << row << ", " << column << ")." << endl;
}


testLocation.cpp
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
#include <iostream>
#include "location.h" 
using namespace std; 

int main () 
{
	
	Location myLocation; 

	cout << "Enter a 3 by 4 two-dimensional array:" << endl; 
	double a [ROW_SIZE] [COLUMN_SIZE]; 
	
	for (int i = 0; i < ROW_SIZE; i++) 
  	{	
      for (int j = 0; j < COLUMN_SIZE; j++) 
      {
          cin >> a [i] [j]; 
      }
 	}         

	myLocation.locateLargest(a);

	system ("PAUSE"); 
	return 0; 

} 

Other than satisfying a somewhat neurotic professor, what would be gained by changing these function types from void to Location? In other words, what would be gained by satisfying the requirement that "The return value is an instance of Location"? What exactly would Location return?

Thank you very much for your assistance!
Last edited on
Hi. There is nothing with the code above. I tried your input and the output was:
The location of the largest element is 12 at (2,3).
Which would make sense if you were to put the first row and column to be zero.

what would be gained by changing these function types from void to Location?

well, you can define another instances of Location using myLocation(= operator). Other than that, nothing really special.
Okay, thanks again for your help!
Topic archived. No new replies allowed.