473,378 Members | 1,314 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,378 software developers and data experts.

Error at compile time: cout undeclared

New problem. I get the following when trying to compile:

in function 'int main()'
'cout' undeclared (first use this function)
(Each undeclared idnetifier is reported only once for each function it appears in.)
'cin' undeclared (first use this function)
In function 'USHORT der(USHORT,USHORT,USHORT,USHORT)'
'(((int)coefficient)*((int)y))' cannot be used as a function

My code (i changed it a bit) is:

Expand|Select|Wrap|Line Numbers
  1.  
  2.   typedef unsigned short USHORT;
  3.   #include <iostream>
  4.   #include <cmath>
  5.   USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z);
  6.  
  7.   int main()
  8.   {
  9.    USHORT coefficient;
  10.    USHORT y;
  11.    USHORT xInteger;
  12.    USHORT z;
  13.    USHORT derivative;
  14.  
  15.    cout << "\nCoefficient of 'X'? ";
  16.    cin >> coefficient;
  17.    cout << "\nPlease enter Exponent? ";
  18.    cin >> y;
  19.    cout << "\nPlease enter x. ";
  20.    cin >> xInteger;
  21.    cout << "\nPlease enter Exponent again? ";
  22.    cin >> z;
  23.  
  24.    derivative= der(coefficient,xInteger,y,z);
  25.  
  26.    cout << "\nYour Derivative is ";
  27.    cout << derivative;
  28.    cout << " Thanks for using this program, hope it helped :)\n\n";
  29.             return 0;
  30.  }
  31.  
  32.  USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z)
  33.  {
  34.       return pow((( coefficient * y ) ( xInteger )) , ( z - 1 ));
  35.  
  36. }
  37.  
NOTE: I have not edited the indentation yet, sorry for that.
Nov 9 '07 #1
15 5539
With the indentations, hope this helps in reading the code. If the indentation is wrong, put me down all you want, all I ask is please teach me! thanks
Expand|Select|Wrap|Line Numbers
  1.  
  2. typedef unsigned short USHORT;
  3. #include <iostream>
  4. #include <cmath>
  5. USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z);
  6.  
  7.  int main()
  8.  {
  9.   USHORT coefficient;
  10.   USHORT y;
  11.   USHORT xInteger;
  12.   USHORT z;
  13.   USHORT derivative;
  14.  
  15.   cout << "\nCoefficient of 'X'? ";
  16.   cin >> coefficient;
  17.   cout << "\nPlease enter Exponent? ";
  18.   cin >> y;
  19.   cout << "\nPlease enter x. ";
  20.   cin >> xInteger;
  21.   cout << "\nPlease enter Exponent again? ";
  22.   cin >> z;
  23.  
  24.   derivative= der(coefficient,xInteger,y,z);
  25.  
  26.   cout << "\nYour Derivative is ";
  27.   cout << derivative;
  28.   cout << " Thanks for using this program, hope it helped :)\n\n";
  29.   return 0;
  30.  }
  31.  
  32.  USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z)
  33.  {
  34.   return pow((( coefficient * y ) ( xInteger )) , ( z - 1 ));
  35.  
  36. }
  37.  
Nov 9 '07 #2
sicarie
4,677 Expert Mod 4TB
beast1945-

As this is a new question, I am putting it in its own thread.
Nov 9 '07 #3
beast1945-

As this is a new question, I am putting it in its own thread.
Ok thanks. For a second I thought I had made another forum.. would not have been a good idea. But now that you say you did it, thanks.
Nov 9 '07 #4
Ganon11
3,652 Expert 2GB
cout, as well as cin, endl, and string (to name a few) are all part of the namespace std. It's not enough to #include <iostream> - you must also use the statement:

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
to let the compiler know you are using variables and classes from the std namespace.

There are 2 alternatives to this, both of which are kind of messy. Since the std namespace has a lot of extras you probably won't use in a simple program like this, you can explicitly say what exactly you wish to include. In this example, you'd need to type:

Expand|Select|Wrap|Line Numbers
  1. using std::cout;
  2. using std::cin;
  3. using std::endl;
Or, forget those using statements. Just refer to cout, cin, and endl by their proper names in your code:

Expand|Select|Wrap|Line Numbers
  1. std::cout << "You can avoid all that using nonsense in this way" << std::endl;
  2. std::cin >> yourVariable;
I prefer the first method - one line, and you're good. But all three solutions will work for you.
Nov 9 '07 #5
oler1s
671 Expert 512MB
It's acceptable for beginners to have a using namespace std; declaration. It certainly makes it easier initially. But eventually you should wean yourself out of a global using directive. It beats the purpose of namespaces in the first place, which is to prevent name clashes. Consider the effect of doing #include <algorithm> and then a using namespace std directive. I've seen a case where someone was surprised that he had a sort function without ever writing it.

Point is, in your source files, in serious code, rely on the qualifiers (std:: , etc.) or keep the directives scoped locally as much as possible, and limited in how much they allow (using std::cout is preferable to dumping the entire namespace).

Also, it's poor form to have using namespace std; in header files. Think for a moment and it should be obvious why this is a disastrous thing to do.
Nov 9 '07 #6
Ok, thanks guys. But let me see if I got this right. For me to use coeffiecient, y, and z as integers I need to put the following in my code:

Expand|Select|Wrap|Line Numbers
  1. coefficient std::cout;
  2. coefficient std::cin;
  3. coefficient std::endl;
  4. y std::cout;
  5. y std::cin;
  6. y std::endl;
  7. z std::cout;
  8. z std::cin;
  9. z std::endl;
  10.  
is tat correct? or am I completely off.
Nov 9 '07 #7
Just to try it out, I took your advice ganon 11, and added
Expand|Select|Wrap|Line Numbers
  1. using namespace std
  2.  
before everything (as line 1) so my new code looks like so:

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

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

I got rid of everything else :), but this still looms :(. i can't figure out why. I used l * w for an area program of mine, which I will list below, and it workedjust fine...

Expand|Select|Wrap|Line Numbers
  1.   typedef unsigned short USHORT;
  2.   #include <iostream.h>
  3.   USHORT FindArea(USHORT length, USHORT width);
  4.  
  5.   int main()
  6.   {
  7.     USHORT lengthOfYard;
  8.    USHORT widthOfYard;
  9.    USHORT areaOfYard;
  10.  
  11.    cout << "\nWidth? ";
  12.    cin >> widthOfYard;
  13.    cout << "\nLength? ";
  14.    cin >> lengthOfYard;
  15.  
  16.    areaOfYard= FindArea(lengthOfYard,widthOfYard);
  17.  
  18.    cout << "\nYour area is ";
  19.    cout << areaOfYard;
  20.    cout << " square feet or meters square (whichever unit of measurment you are using)\n\n";
  21.             return 0;
  22.  }
  23.  
  24.  USHORT FindArea(USHORT l, USHORT w)
  25.  {
  26.       return l * w;
  27. }
  28.  
  29.  
PLEASE NOTE: this is one of the first codes I made, it does have problems, indentations being one of them. But it compiled just fine, and works just fine. i've used it to test it out many times. please tell me why my derivative code keeps giving me that compile error! Thanks guys.
Nov 9 '07 #8
oler1s
671 Expert 512MB
And my modifications to your code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int der(int coefficient, int xInteger, int y, int z);
  5.  
  6. int main()
  7. {
  8.     int coefficient;
  9.     int y;
  10.     int xInteger;
  11.     int z;
  12.     int derivative;
  13.  
  14.     cout << "\nCoefficient of 'X'? ";
  15.     cin >> coefficient;
  16.     cout << "\nPlease enter Exponent? ";
  17.     cin >> y;
  18.     cout << "\nPlease enter x. ";
  19.     cin >> xInteger;
  20.     cout << "\nPlease enter Exponent again? ";
  21.     cin >> z;
  22.  
  23.     derivative= der(coefficient,xInteger,y,z);
  24.  
  25.     cout << "\nYour Derivative is ";
  26.     cout << derivative;
  27.     cout << " Thanks for using this program, hope it helped :)\n\n";
  28.     return 0;
  29. }
  30.  
  31. int der(int coefficient, int xInteger, int y, int z)
  32. {
  33.     return pow((( coefficient * y ) * ( xInteger )) , ( z - 1 ));
  34.  
  35. }
  36.  
I fixed the indentation with astyle. It's a pretty handy program to have around. There's a GUI version called AstyleWin that's floating around as well.
Nov 10 '07 #9
Oh wow, thanks man. So my problem was symply not multiplying th e xInteger and the (coefficient*y)?? And thanks for the inentation software names, i'll try those.
Nov 11 '07 #10
omg... Still wont compile. Ok besides DEV-C++, are there any compilers that are really good? like what do you use?
Nov 11 '07 #11
Ganon11
3,652 Expert 2GB
Don't blame your compiler for your program not working. Any compiler should be fine, and I know for a fact Dev C++ is fine. What errors are you getting?
Nov 11 '07 #12
oler1s
671 Expert 512MB
omg... Still wont compile. Ok besides DEV-C++, are there any compilers that are really good? like what do you use?
So your code isn’t compiling, because your compiler is at fault...? Really? What do you expect a different compiler to do? Feed it incorrect code, and it will respond the same way: with lots of errors. Dev-C++ uses MinGW as its compiler. MinGW is the windows port of gcc, and gcc, well, I’ll leave it as a googling exercise for you to figure out exactly how many people are using it...

You need to develop a systematic method of dealing with compile errors. They are frustrating yes, but if you keep throwing up your hands every time you see one, then you won’t get very far. You’ll have a lot initially as a beginner, as you don’t have the experience and knowledge to avoid or deal with them efficiently. But try your best to understand how to work and fix those errors.

In this case, you probably got some error about pow and overloaded functions. pow has been overloaded to take many different forms of parameters, like (double,int), (float,float) and the like. Oddly enough, there is no (int,int) form. So you need to cast the first parameter. A double will do. static_cast<double>(the first parameter) should work fine.
Nov 11 '07 #13
1. make sure you have "using namespace std;" underneath your included libraries

2. In your der( ) function, try doing the calculations first then return that single value, it will pass an unsigned short because that is what you declared the function as...

Try declaring the function as double or float.

Expand|Select|Wrap|Line Numbers
  1. typedef unsigned short USHORT;
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. double der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z);
  7.  
  8. int main()
  9. {
  10.          USHORT coefficient;
  11.          USHORT y;
  12.          USHORT xInteger;
  13.          USHORT z;
  14.          USHORT derivative;
  15.  
  16.          cout << "\nCoefficient of 'X'? ";
  17.          cin >> coefficient;
  18.          cout << "\nPlease enter Exponent? ";
  19.          cin >> y;
  20.          cout << "\nPlease enter x. ";
  21.          cin >> xInteger;
  22.          cout << "\nPlease enter Exponent again? ";
  23.          cin >> z;
  24.  
  25.          derivative= der(coefficient,xInteger,y,z);
  26.  
  27.          cout << "\nYour Derivative is ";
  28.          cout << derivative;
  29.          cout << " Thanks for using this program, hope it helped :)\n\n";
  30.                   return 0;
  31. }
  32.  
  33. double der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z)
  34. {
  35.      double calc;
  36.       calc = pow((( coefficient * y ) ( xInteger )) , ( z - 1 ));//syntax or logic error here, NOT compiler's fault, cant help you much im not big on math and derivatives, it says it doesnt evaluate to a function so it must be coded incorrectly
  37.      return calc;
  38.  
  39. }
Nov 11 '07 #14
TheVith's code works fine, but it still says "'(coefficient * y)' cannot be used as a function" Thats the only remaining problem. I was thinking, what if i seperate the der function into sub parts then make derivative= all the sub answers put together...would that work? Also, why do i get that error, all the time?
Nov 13 '07 #15
Ganon11
3,652 Expert 2GB
It's giving you that error basically because you have this situation:

pow((( coefficient * y ) ( xInteger )) , ( z - 1 ));

What do you see between ( coeffecient * y ) and ( xInteger )? I don't see anything. Is there a * operator there? A ^ (which you'd need pow() again for)? A +? A <<?
Nov 14 '07 #16

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

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
0
by: Zach | last post by:
Using Dev-C++ 4.9.8.5 I get error: Compiler: Default compiler Executing g++.exe... g++.exe "C:\winroot\src\c++\stupid_tricks\functor_trick.cpp" -o...
6
by: deancoo | last post by:
Hello all, I'm having a problem compiling some code which I believe should compile. Can someone tell me what I'm doing wrong. The code in question is appended below, followed by the compiler...
3
by: Materialised | last post by:
I am having some issues with the following code, basically what I am trying to do, is familierise myself with compiler warnings, when I try to access a private or protected class function. Here is...
5
by: cranium.2003 | last post by:
hi, Here is my code #include <iostream.h> int main() { cout <<"HI"; return 0; } and using following command to compile a C++ program g++ ex1.cpp -o ex1
16
by: Jdban101 | last post by:
I am working on my second program ever, so bear with me. When I try to compile it, I get two error messages(sort of 3): In function `int main()': line 6: `endl' undeclared (first use this...
2
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
2
by: KiranJyothi | last post by:
Can anyone please tell me how can I rectify my compile time error in this program. /********************************HEADER FILE***********************************/ #ifndef POLYNOMIAL_H //...
2
by: zelmila19 | last post by:
Hi, I'm doing C++ with visual studios 2005 and recently did this program #include <iostream> int num1; int num2; int answer; char operation;
25
by: notahipee | last post by:
I have been trying to cin an number from 0 to 9 with a leading 0. For example 00 or 07. I was using a switch case. switch (int) { case 01: break; case 02: break;..... My problem arises at 08...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.