Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have just started learning C++, I come from a C# background. Below is some code where I am trying to cal an overloaded method, but I get the error "identifier not found"? Please be gentle:

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain()
{
	char input = '0';
	while(input != 'd' && input != 'g')
	{
		cout << "Please select a company to recieve a quote from\nDHL - (d)\nGlobalMail - (g)\n\n(select either g or d)->";
		cin >> input;
	}

	cout << "\n\n";
	
	switch (input)
	{
	case 'd':
		
         break;

	case 'g':
		cout << "GlobalMail, great choice! Please enter the weight of your parcel\n->";
		int weight = NULL;
		cin >> weight;
		weight = calcPostage(weight); //this line breaks
		cout << "Your package will cost R" << weight;
		break;
	}

	return 0;
}

int calcPostage(int weight)
{
	return weight * 108;
}

int calcPostage(int length, int width, int height)
{
	return (length * width * height) / 5000;
}</iostream>
Posted

You need to mention the function prototype for overloaded calcPostage functions before main,that will solve the issue.
 
Share this answer
 
Comments
DominicZA 4-Aug-11 4:22am    
ok, Im new...what does this mean? How do you do this?
hakz.code 4-Aug-11 4:26am    
put the following statements before main.
int calcPostage(int);
int calcPostage(int,int,int);
Sergey Alexandrovich Kryukov 7-Aug-11 4:08am    
Ha-ha, good catch, a 5.
--SA
The compiler works from the top of a source file to the bottom.

A function prototype is a definition of the function that will exist and so the compiler knows not to fail the code compile.

If it finds a function that is not defined already it will fail, in the same way it will fail if a function is defined twice.

As Harish has said putting:

int calcPostage(int);
int calcPostage(int,int,int);

before the main will fix the code so that the compiler knows that these function will exist at some point in the future in the code.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Aug-11 4:08am    
Non-nonsense explanation; it looks like you took into account apparent OP's level, good, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900