Error at compile time: cout undeclared

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beast1945
    New Member
    • Nov 2007
    • 13

    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,USHO RT,USHORT,USHOR T)'
    '(((int)coeffic ient)*((int)y)) ' cannot be used as a function

    My code (i changed it a bit) is:

    Code:
      typedef unsigned short USHORT;
      #include <iostream>
      #include <cmath>
      USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z);
    
      int main()
      {
       USHORT coefficient;
       USHORT y;
       USHORT xInteger;
       USHORT z;
       USHORT derivative;
    
       cout << "\nCoefficient of 'X'? ";
       cin >> coefficient;
       cout << "\nPlease enter Exponent? ";
       cin >> y;
       cout << "\nPlease enter x. ";
       cin >> xInteger;
       cout << "\nPlease enter Exponent again? ";
       cin >> z;
    
       derivative= der(coefficient,xInteger,y,z);
    
       cout << "\nYour Derivative is ";
       cout << derivative;
       cout << " Thanks for using this program, hope it helped :)\n\n";
                return 0;
     }
    
     USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z)
     {
          return pow((( coefficient * y ) ( xInteger )) , ( z - 1 ));
          
    }
    NOTE: I have not edited the indentation yet, sorry for that.
    Last edited by beast1945; Nov 9 '07, 06:05 AM. Reason: NOTE: added
  • beast1945
    New Member
    • Nov 2007
    • 13

    #2
    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
    Code:
    typedef unsigned short USHORT;
    #include <iostream>
    #include <cmath>
    USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z);
    
     int main()
     {
      USHORT coefficient;
      USHORT y;
      USHORT xInteger;
      USHORT z;
      USHORT derivative;
    
      cout << "\nCoefficient of 'X'? ";
      cin >> coefficient;
      cout << "\nPlease enter Exponent? ";
      cin >> y;
      cout << "\nPlease enter x. ";
      cin >> xInteger;
      cout << "\nPlease enter Exponent again? ";
      cin >> z;
    
      derivative= der(coefficient,xInteger,y,z);
    
      cout << "\nYour Derivative is ";
      cout << derivative;
      cout << " Thanks for using this program, hope it helped :)\n\n";
      return 0;
     }
    
     USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z)
     {
      return pow((( coefficient * y ) ( xInteger )) , ( z - 1 ));
          
    }

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      beast1945-

      As this is a new question, I am putting it in its own thread.

      Comment

      • beast1945
        New Member
        • Nov 2007
        • 13

        #4
        Originally posted by sicarie
        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.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          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:

          [CODE=cpp]using namespace std;[/CODE]

          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:

          [CODE=cpp]using std::cout;
          using std::cin;
          using std::endl;[/CODE]

          Or, forget those using statements. Just refer to cout, cin, and endl by their proper names in your code:

          [CODE=cpp]std::cout << "You can avoid all that using nonsense in this way" << std::endl;
          std::cin >> yourVariable;[/CODE]

          I prefer the first method - one line, and you're good. But all three solutions will work for you.

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            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.

            Comment

            • beast1945
              New Member
              • Nov 2007
              • 13

              #7
              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:

              Code:
              coefficient std::cout;
              coefficient std::cin;
              coefficient std::endl;
              y std::cout;
              y std::cin;
              y std::endl;
              z std::cout;
              z std::cin;
              z std::endl;
              is tat correct? or am I completely off.

              Comment

              • beast1945
                New Member
                • Nov 2007
                • 13

                #8
                Just to try it out, I took your advice ganon 11, and added
                Code:
                using namespace std
                before everything (as line 1) so my new code looks like so:

                Code:
                using namespace std;
                typedef unsigned short USHORT;
                #include <iostream>
                #include <cmath>
                USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z);
                
                 int main()
                 {
                  USHORT coefficient;
                  USHORT y;
                  USHORT xInteger;
                  USHORT z;
                  USHORT derivative;
                
                  cout << "\nCoefficient of 'X'? ";
                  cin >> coefficient;
                  cout << "\nPlease enter Exponent? ";
                  cin >> y;
                  cout << "\nPlease enter x. ";
                  cin >> xInteger;
                  cout << "\nPlease enter Exponent again? ";
                  cin >> z;
                
                  derivative= der(coefficient,xInteger,y,z);
                
                  cout << "\nYour Derivative is ";
                  cout << derivative;
                  cout << " Thanks for using this program, hope it helped :)\n\n";
                  return 0;
                 }
                
                 USHORT der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z)
                 {
                  return pow((( coefficient * y ) ( xInteger )) , ( z - 1 ));
                      
                }
                Now I get the following compile error:

                In function 'USHORT der(USHORT,USHO RT,USHORT,USHOR T)':
                '(((int)coeffic ient) * ((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...

                Code:
                  typedef unsigned short USHORT;
                  #include <iostream.h>
                  USHORT FindArea(USHORT length, USHORT width);
                
                  int main()
                  {
                    USHORT lengthOfYard;
                   USHORT widthOfYard;
                   USHORT areaOfYard;
                
                   cout << "\nWidth? ";
                   cin >> widthOfYard;
                   cout << "\nLength? ";
                   cin >> lengthOfYard;
                
                   areaOfYard= FindArea(lengthOfYard,widthOfYard);
                
                   cout << "\nYour area is ";
                   cout << areaOfYard;
                   cout << " square feet or meters square (whichever unit of measurment you are using)\n\n";
                            return 0;
                 }
                
                 USHORT FindArea(USHORT l, USHORT w)
                 {
                      return l * w;
                }
                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.

                Comment

                • oler1s
                  Recognized Expert Contributor
                  • Aug 2007
                  • 671

                  #9
                  And my modifications to your code:

                  [CODE=cpp]
                  #include <iostream>
                  #include <cmath>
                  using namespace std;
                  int der(int coefficient, int xInteger, int y, int z);

                  int main()
                  {
                  int coefficient;
                  int y;
                  int xInteger;
                  int z;
                  int derivative;

                  cout << "\nCoeffici ent of 'X'? ";
                  cin >> coefficient;
                  cout << "\nPlease enter Exponent? ";
                  cin >> y;
                  cout << "\nPlease enter x. ";
                  cin >> xInteger;
                  cout << "\nPlease enter Exponent again? ";
                  cin >> z;

                  derivative= der(coefficient ,xInteger,y,z);

                  cout << "\nYour Derivative is ";
                  cout << derivative;
                  cout << " Thanks for using this program, hope it helped :)\n\n";
                  return 0;
                  }

                  int der(int coefficient, int xInteger, int y, int z)
                  {
                  return pow((( coefficient * y ) * ( xInteger )) , ( z - 1 ));

                  }
                  [/CODE]

                  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.

                  Comment

                  • beast1945
                    New Member
                    • Nov 2007
                    • 13

                    #10
                    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.

                    Comment

                    • beast1945
                      New Member
                      • Nov 2007
                      • 13

                      #11
                      omg... Still wont compile. Ok besides DEV-C++, are there any compilers that are really good? like what do you use?

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        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?

                        Comment

                        • oler1s
                          Recognized Expert Contributor
                          • Aug 2007
                          • 671

                          #13
                          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<dou ble>(the first parameter) should work fine.

                          Comment

                          • TheVith
                            New Member
                            • Nov 2007
                            • 5

                            #14
                            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.

                            Code:
                            typedef unsigned short USHORT;
                            #include <iostream>
                            #include <cmath>
                            using namespace std;
                            		
                            double der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z);
                                   
                            int main()
                            {
                                     USHORT coefficient;
                                     USHORT y;
                                     USHORT xInteger;
                                     USHORT z;
                                     USHORT derivative;
                                   
                                     cout << "\nCoefficient of 'X'? ";
                                     cin >> coefficient;
                                     cout << "\nPlease enter Exponent? ";
                                     cin >> y;
                                     cout << "\nPlease enter x. ";
                                     cin >> xInteger;
                                     cout << "\nPlease enter Exponent again? ";
                                     cin >> z;
                                   
                                     derivative= der(coefficient,xInteger,y,z);
                                   
                                     cout << "\nYour Derivative is ";
                                     cout << derivative;
                                     cout << " Thanks for using this program, hope it helped :)\n\n";
                                              return 0;
                            }
                                   
                            double der(USHORT coefficient, USHORT xInteger, USHORT y, USHORT z)
                            {
                                 double calc;
                                  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
                                 return calc;
                                       
                            }

                            Comment

                            • beast1945
                              New Member
                              • Nov 2007
                              • 13

                              #15
                              TheVith's code works fine, but it still says "'(coeffici ent * 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...woul d that work? Also, why do i get that error, all the time?

                              Comment

                              Working...