473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Can't seem to be able to get my compiler to compile

So i wrote a code that would tell the user a given derivative for the inputed data. Well it seems to have a problem I don't understand how to fix. I had other problems which i fixed but this one perplexes me. Basically my code looks like so:


Expand|Select|Wrap|Line Numbers
  1.  typedef unsigned short USHORT;
  2. #include <iostream.h>
  3. USHORT der(USHORT x, USHORT xInteger, USHORT y, USHORT z);
  4.  
  5. int main()
  6. {
  7. USHORT x;
  8. USHORT y;
  9. USHORT xInteger;
  10. USHORT z;
  11. USHORT derivative;
  12.  
  13. cout << "\nCoefficient of 'X'? ";
  14. cin >> x;
  15. cout << "\nPlease enter Exponent? ";
  16. cin >> y;
  17. cout << "\nPlease enter x. ";
  18. cin >> xInteger;
  19. cout << "\nPlease enter Exponent again? ";
  20. cin >> z;
  21.  
  22. derivative= der(x,xInteger,y,z);
  23.  
  24. cout << "\nYour Derivative is ";
  25. cout << derivative;
  26. cout << " Thanks for using this program, hope it helped :)\n\n";
  27. return 0;
  28. }
  29.  
  30. USHORT der(USHORT x, USHORT xInteger, USHORT y, USHORT z)
  31. {
  32. return ( x * y ) ( xInteger ) ^ ( z - 1 );
  33.  
  34. }
Now on the bottom of my compiler (I use Dev-C++) it writes:

In function 'USHORT der(USHORT,USHORT,USHORT,USHORT).'
'(((int)x)*((int)y))' cannot be used as a function

Why am I getting this message? If anyone knows please help me! Thank you.

Below is a picture of what my screen looked like after I attempted to compile this.

http://i172.photobucket.com/albums/w...pleasehelp.jpg
Nov 8 '07 #1
8 1466
oler1s
671 Expert 512MB
I’ll tell you how to fix your code, but before that...

What’s with the unsigned shorts? You can’t construct a basic mathematical expression in C++, but you have typedefed a ushort, and then used it where any beginner would have used an int. In fact, I probably would have used an int anyway, as a short might have been more inefficient.

It’s <iostream>, not <iostream.h> . Get an up-to-date learning resource, because <iostream.h> is the old form of the standard library headers. It was changed nearly a decade ago.

There’s indentation problems. Looks like you have a bad text editor. Get a half decent one.

In C++, and most programming languages, you can’t write mathematical expressions like you would on paper. (5)(6) is not 5*6. You have to explicitly have that * there.

Furthermore, ^ does not mean power. It means bitwise XOR. I’ll leave it as a google exercise to you to figure out how to use a power in C++.

Oh, and, while giving us a snapshot of your Dev-C++ IDE was a bit unusual and eye opening, next time, you can just give us the compiler outputs. Make sure you turn all warnings in the compile settings.
Nov 8 '07 #2
Sorry I'm a bit new to this, and I was looking online for help, and I got that old one (the <iostream.h>). Anyway I found an article saying that the power rule was like 5^6 would be 5*5*5*5*5*5, correct? As for the indentations what do you mean? Thanks for the reply by the way. One more question. When you say the thing about the Unsigned shorts, I thought I could use typedef, to name it what I wanted. maybe I need a more up to date book on C++...If you know any could you refer me?
Nov 8 '07 #3
oler1s
671 Expert 512MB
Sorry I’m a bit new to this, and I was looking online for help, and I got that old one
No need to apologize. Most resources are outdated. C++ has been around for a while, and the internet did exist before 1999. <iostream.h> is a good sign that the resource is outdated. Much of the material is probably still valid thought, FYI.

Anyway I found an article saying that the power rule was like 5^6 would be 5*5*5*5*5*5, correct?
I’m not entirely sure how to interpret this statement. Obviously, power means repeated multiplication. This is a basic arithmetic operation.

So perhaps you’re asking if in C++, x^y means power, the answer is no. Programming wise, ^ tends to mean bitwise XOR. It’s true for C++ and for most other programming languages. To take a power in C++, you actually need to use pow(...). It’s declared in the header <cmath>.

As for the indentations what do you mean?
Take for example that your typedef has no indentation, but your includes do. int main() is indented by 2 spaces. But everything in main uses 1 space indentation(?). Your return 0 is randomly indented. See what I mean? While C++ doesn’t really care about indentation, there’s good reasons you want to maintain consistency.

For one thing, it makes your code more readable. Proper formatting means we can follow the code easily, and it takes less effort for both you and other people to read and edit it. Formatted code means that errors are then easier to spot. Many beginners who are sloppy with indentation also end up having trouble keeping track of braces. That shouldn’t even be an issue.

If you rely completely on an IDE, then indentation errors are from sheer sloppiness. If you move code between an IDE and a text editor, then make sure you have a decent text editor. Notepad is not acceptable.

When you say the thing about the Unsigned shorts, I thought I could use typedef, to name it what I wanted.
Yes, but my question did not say that your syntax is wrong. typedefs are a pretty common thing, both to simplify and for readability.

I’m puzzled as to why you specifically chose to use an unsigned short. Most people would have stuck with an int. If someone really wanted to, they would have a uint. But a ushort? I’m actually curious as to your logic here.

maybe I need a more up to date book on C++...If you know any could you refer me?
There’s two good beginner level books. Accelerated C++ by Koenig, and C++ Primer by Lippman. Get the latest edition of them if you intend to do so. I think most experienced C++ programmers will second my recommendation. The books are quality, but they are also technically accurate. The authors are reputed in the C++ sphere, so one trusts them to actually be accurate. Primer is more like a reference book, and AC++ more like a tutorial/coursework book.
Nov 9 '07 #4
Wow, fast reply, thanks for the book references btw. ok so for the USHORT, it was in the book I was reading...that's what they named it. As I get better I guess i'll learn what to use and what not to. Ok as for indentation, i need a text editor. I believe that DEV-C++ has one, if I'm wrong which ones would you recommend? As for teh power rule, I found an article and came here to reply that I was wrong, but I didn't know about the <cmath>, could you elaborate as to where I put that? Is it above main ()? Or within it? thanks a lot, great help you have been :).
Nov 9 '07 #5
Ganon11
3,652 Expert 2GB
You would include <cmath> exactly like you include <iostream> - in fact, right after that #include statement would be fine.

I actually learned C++ using Dev 4.9.9.2, and I am familiar with the IDE you're using. It's indenting works fine if you let it. As oler1s said, the inconsistency there is due to something on your end, not the IDE. Just remember to indent your code properly, and you should be fine using that IDE.
Nov 9 '07 #6
Well, thanks so much. you guys are a great help. I'd like to ask you a question Gannon. How exactly did you learn C++ and master it, and when did you start? Just so I could get a picture of how to get to your position. oh and as for the rest o the code, anything else wrong, besides the pw(..) thing, and indentation.
Nov 9 '07 #7
Ganon11
3,652 Expert 2GB
I'd like to ask you a question Gannon. How exactly did you learn C++ and master it, and when did you start?
I started learning C++ at the beginning of my senior year in High School, which was about 14 months ago. However, I started learning the programming concepts I know now about a year earlier, learning them in Java. A year before that, I was learning Visual Basic. So, as of right now, I've had 3+ years of programming experience. And in no way have I mastered any of these languages. As with any topic, it doesn't matter how smart you are - there's something else to learn.

Currently, I'm in my freshman year of college, studying for my degree in Computer Science. I've got 3.5 years ahead of me to continue learning C++ and programming techniques, and even after that, I won't be a 'master' of it.

What I suggest to you is lots of practice. It's not enough to read how to write a function, for example - find some exercises, write some functions, see what works, and see what doesn't. That said, pay attention to any textbook you're reading - they usually know their stuff, and will write good code that's easy to understand. Finally, understand the basics. If you're ever unclear on something, don't pass by - work on it until you get it. If reading the book doesn't help, try programming with it. If you still don't get it after writing a few programs, ask for help.

Are you in a programming class at college or otherwise? If so, your professor/teacher will be more than willing to help you.
Nov 9 '07 #8
I'm in my senior year in High school. So, not far behind oyu as in terms of college but very behind in programing. Are there any sites you would recommend that I use to help me reach a higher level of understanding?
Nov 10 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
9
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.