Can anyone tell me what this error means?

CraKaJaX

Lifer
Dec 26, 2004
11,905
148
101
Doing some homework and I'm getting an error I've never seen before. I'm only in Intro to C++, so it's only been like 4 weeks.. but still :p I'm defining my own variable at the top of the program as:

int totalPrice(int numberStocks, int numerator, int denominator);



and at the bottom of the program as:

int totalPrice(int numberStocks, int numerator, int denominator)
{
double fraction = static_cast<double>(numerator)/static_cast<double>(denominator);

return (numberStocks+fraction);
}


It's giving me an error that says: "illegal, left operand has type 'int (__cdecl *)(int,int,int)"

How do I fix this? I read the section for user-defined variables over and over but still can't figure out what's wrong? Tried googling it, but that didn't help at all. Any help is appreciated.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Pardon the patronization:
First of all, this is a function declaration:
int totalPrice(int numberStocks, int numerator, int denominator);

This is a function definition:

int totalPrice(int numberStocks, int numerator, int denominator)
{
double fraction = static_cast<double>(numerator)/static_cast<double>(denominator);

return (numberStocks+fraction);
}

You'll want to get your terminology right so we can help you more easily. (These aren't variable declarations/definitions)

Moving on...


The error your seeing arises because you're using an identifier (the name of something, e.g. fraction, numerator, totalPrice are all identifiers) which happens to be the name of a function in a place you shouldn't be using it. Most compilers will also spit out line number to tell you where the problem is -- in your code snipped, I do not see anything wrong, so you probably didn't include the right part.

You're going to have to provide more of the code -- I don't think your error lies in the above section.
In the future, you should also tell us which platform you're on -- it looks like Win-something by the __cdecl*, but it may not always be so obvious and it often matters.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: IHateMyJob2004
I know people might yell at me, but just do this:
double fraction = (1.0*numerator)/denominator;

ZOMG HOW CAN YOU DO THAT!?!?!?

J/K. In all seriousness, I always use C-style casts or similar tricks instead of static_cast.
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
Originally posted by: degibson
Originally posted by: IHateMyJob2004
I know people might yell at me, but just do this:
double fraction = (1.0*numerator)/denominator;

ZOMG HOW CAN YOU DO THAT!?!?!?

J/K. In all seriousness, I always use C-style casts or similar tricks instead of static_cast.

I agree. C++ static casts are, in all honesty, really ugly. If I have to cast it is almost always (double)someInt.
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Originally posted by: Cogman
Originally posted by: degibson
Originally posted by: IHateMyJob2004
I know people might yell at me, but just do this:
double fraction = (1.0*numerator)/denominator;

ZOMG HOW CAN YOU DO THAT!?!?!?

J/K. In all seriousness, I always use C-style casts or similar tricks instead of static_cast.

I agree. C++ static casts are, in all honesty, really ugly. If I have to cast it is almost always (double)someInt.

I'd agree. However, in an academic setting, you'll get in trouble for using something the teacher has not yet taught. I know I ran into that a lot when I was in my intro programming classes some years ago.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: drebo
Originally posted by: Cogman
Originally posted by: degibson
Originally posted by: IHateMyJob2004
I know people might yell at me, but just do this:
double fraction = (1.0*numerator)/denominator;

ZOMG HOW CAN YOU DO THAT!?!?!?

J/K. In all seriousness, I always use C-style casts or similar tricks instead of static_cast.

I agree. C++ static casts are, in all honesty, really ugly. If I have to cast it is almost always (double)someInt.

I'd agree. However, in an academic setting, you'll get in trouble for using something the teacher has not yet taught. I know I ran into that a lot when I was in my intro programming classes some years ago.

Yes, that is indeed true and lame. I was pretty fortunate to have mostly non-jerk instructors in my lower-level programming courses, who would tolerate me 'reading ahead', as it were.
 
Sep 29, 2004
18,656
68
91
Originally posted by: degibson
Originally posted by: drebo
Originally posted by: Cogman
Originally posted by: degibson
Originally posted by: IHateMyJob2004
I know people might yell at me, but just do this:
double fraction = (1.0*numerator)/denominator;

ZOMG HOW CAN YOU DO THAT!?!?!?

J/K. In all seriousness, I always use C-style casts or similar tricks instead of static_cast.

I agree. C++ static casts are, in all honesty, really ugly. If I have to cast it is almost always (double)someInt.

I'd agree. However, in an academic setting, you'll get in trouble for using something the teacher has not yet taught. I know I ran into that a lot when I was in my intro programming classes some years ago.

Yes, that is indeed true and lame. I was pretty fortunate to have mostly non-jerk instructors in my lower-level programming courses, who would tolerate me 'reading ahead', as it were.

Teachers should not be concerned with the semantics of a programming language unless the class is for a specific language and specific IDE and specific compiler. So, one you are past freshman year, the concepts should be all that matters (and well commented code).

What sucks is that even in industry, people do not comment nearly enough. People think there code is perfect and will never be maintained but I can guarantee that all code is maintained eventually and usually not by the original author. And even if it is maintained by the original author, they do not reemmber what they did 5 years previously.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: IHateMyJob2004
Originally posted by: degibson
Originally posted by: drebo
Originally posted by: Cogman
Originally posted by: degibson
Originally posted by: IHateMyJob2004
I know people might yell at me, but just do this:
double fraction = (1.0*numerator)/denominator;

ZOMG HOW CAN YOU DO THAT!?!?!?

J/K. In all seriousness, I always use C-style casts or similar tricks instead of static_cast.

I agree. C++ static casts are, in all honesty, really ugly. If I have to cast it is almost always (double)someInt.

I'd agree. However, in an academic setting, you'll get in trouble for using something the teacher has not yet taught. I know I ran into that a lot when I was in my intro programming classes some years ago.

Yes, that is indeed true and lame. I was pretty fortunate to have mostly non-jerk instructors in my lower-level programming courses, who would tolerate me 'reading ahead', as it were.

Teachers should not be concerned with the semantics of a programming language unless the class is for a specific language and specific IDE and specific compiler. So, one you are past freshman year, the concepts should be all that matters (and well commented code).

What sucks is that even in industry, people do not comment nearly enough. People think there code is perfect and will never be maintained but I can guarantee that all code is maintained eventually and usually not by the original author. And even if it is maintained by the original author, they do not reemmber what they did 5 years previously.

The original author is luck to remember 6 months ago if they have moved onto another project.

But you are preaching here to the choir