Jump to content


compiler error code c2440


  • You cannot reply to this topic
4 replies to this topic

#1 CollegeProginTraining

    New Member

  • Members
  • Pip
  • 4 posts

Posted 31 October 2006 - 12:25 AM

I am getting a compiler error code C2440 when I try to build this program.
Any help is appreciated.

/* Euclidean.cpp


	Writing the Euclidean algorithm using C++.



10-29-06

Lab 7

*/


#include<iostream>

using namespace std;


int gcd( int & a, int & b, int & c);

int main(){

	int a, b, c;

	cout << "Enter the first number: ";

	cin >> a;

	cout << "Enter the second number: ";

	cin >> b;

	return gcd;

}

int gcd( int & a, int & b, int & c){

	if (b = 0){

		b = a;

	}

	else{

		a = b;

		c = a % b;

		b = c;

	}

	return a/c;

}



Output results:

1>------ Build started: Project: Lab7, Configuration: Debug Win32 ------

1>Compiling...

1>Euclidean.cpp

1>c:\comp128\lab7\lab7\euclidean.cpp(20) : error C2440: 'return' : cannot convert from 'int (__cdecl *)(int &,int &,int &)' to 'int'

1>        There is no context in which this conversion is possible

1>Build log was saved at "file://c:\Comp128\Lab7\Lab7\Debug\BuildLog.htm"

1>Lab7 - 1 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



#2 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 31 October 2006 - 01:07 AM

You are attempting to return the address of your gcd function. Replace the line "return gcd;" with "return gcd(a, b, c);".

Try reading up on variable scoping and function calls. GIYF
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#3 pater

    Valued Member

  • Members
  • PipPipPip
  • 117 posts

Posted 31 October 2006 - 07:02 AM

BTW: You should not pass the arguments by reference to your gcd function. In this simple program, you're not using the variables after the call, so it doesn't matter, but as soon as you'll be doing anything more complicated, you'll get wrong results on any further computation.
Replace int gcd(int &a, int &b, int &c) by int gcd(int a, int b) both times. C is only used as local var, so it hasn't got anything to do in the method header. Declare it locally.
Besides, I doubt your algorithm is correct. There must be some loop or recursion in there. And "=" is not a comparison operator.

#4 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 911 posts

Posted 31 October 2006 - 12:00 PM

Also, c is never initialized in main. If the user passes 0 as b, you get a division with an uninitialized variable, and then you're screwed. But on the other hand, if the user doesn't pass 0 to b then you will get a division by zero, in which case you are also screwed. :)
"Stupid bug! You go squish now!!" - Homer Simpson

#5 dega512

    Valued Member

  • Members
  • PipPipPip
  • 108 posts

Posted 31 October 2006 - 06:30 PM

Watch out, right now you are returning whatever gcf returns as an error code when you exit the program.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users