Thread: Errors that make no sense...

  1. #1
    High school loser
    Join Date
    Dec 2004
    Posts
    27

    Errors that make no sense...

    Everything I touch in programming gets ruined. Errors all over the place.
    It's another group project, but this time we have to present it to the class and it refuses to compile. We don't even understand the errors. The program is supposed to return the distance between two input coordinates.
    Here's what we have so far:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    
    using namespace std;
    int GetCoordinate ();
    double GetDistance (double X1, double Y1, double X2, double Y2);
    void PrintResults (/*double d, double X1, double Y1, double X2, double Y2*/);
    
    int main(int argc, char *argv[])
    {
      double X1=0;
      double Y1=0;
      double X2=0;
      double Y2=0;
      double d;
      GetCoordinate();
      d = GetDistance (double X1, double Y1, double X2, double Y2);
      PrintResults();
    system ("PAUSE");
      return 0;
    }  
    
    int GetCoordinate ();
        {
            cout << "Enter the coordinates of two points (X1,X2),(Y1,Y2)" << endl;
            cin >>X1>>X2>>Y1>>Y2;
        }
        
        double GetDistance /*(double X1, double Y1, double X2, double Y2, double d)*/;
        {
            double d = sqrt(pow((Y2-Y1), 2.0) + pow((X2-X1), 2.0));
        }
        
        void PrintResults (double d, int X1, int Y1, int X2, int Y2);
        {
            cout << "The distance between ("<<X1<<", "<<X2<<") and" << endl;
            cout<< "("<<Y1<<", "<<Y2<<") is "<<d<<"." << endl;
        }
    Here is our collection of errors that we cannot sort out.
    • N:\distancecode.cpp In function `int main(int, char**)':
    • 18 N:\distancecode.cpp syntax error before `,' token
    • distancecode.cpp N:\distancecode.cpp At global scope:
    • 25 N:\distancecode.cpp syntax error before `{' token
    • 27 N:\distancecode.cpp syntax error before `>>' token
    • 30 N:\distancecode.cpp `double GetDistance' redeclared as different kind of symbol
    • 7 N:\distancecode.cpp previous declaration of `double GetDistance(double, double, double, double)'
    • 31 N:\distancecode.cpp syntax error before `{' token
    • 36 N:\distancecode.cpp syntax error before `{' token
    • 38 N:\distancecode.cpp syntax error before `<<' token
    • [Linker error] undefined reference to `GetDistance(double, double, double, double)'

    Maybe these are really easy to fix but we're too inexperienced to figure out what to do. If someone would please shed some light on these errors we'd be much obliged.
    "Confused by previous errors. Bailing out." --Bloodshed DevC++ compiler

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    18 N:\distancecode.cpp syntax error before `,' token

    Don't need to include data types when calling a function, hence no 'double's


    Also, don't put a semicolon at the end of the function name when you are defining it. and don't comment out the parameter list.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Start with this:
    Code:
    double GetDistance /*(double X1, double Y1, double X2, double Y2, double d)*/;
    Format in C++ is very specific and it must be exact. You cannot define a function like this:

    double SomeFunc;

    A function definition looks like this:
    Code:
    double SomeFunc(double p1, double p2)
    {
          //lines of code
    }
    If you are that inexperienced, I suggest that C++ is not the language you should be using for your project.
    Last edited by 7stud; 02-28-2005 at 10:50 AM.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Several errors that I see, one thats common and one that really confuses me. The common error is that you have semicolons after your function definitions, the ones after main, not before. Basically when you put the semicolons there you are saying your definition is done thus they aren't getting defined and the compiler views the code in the braces as extraneous.

    The second error which I don't understand is where you comment out the variables in PrintResults before main and in GetDistance after main. Take out the /* and the */ as well as the semicolons I mentioned above, recompile and let us know what the new results are.

    [edit] doh, beaten not once, but twice lol [/edit]

  5. #5
    High school loser
    Join Date
    Dec 2004
    Posts
    27
    We wouldn't do the project in C++ except it's a C++ class.
    Originally the school advertised it as being a class in BASIC, but the teacher decided he liked this language better despite all of us learning nearly nothing.
    "Confused by previous errors. Bailing out." --Bloodshed DevC++ compiler

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Also, a couple of other errors that I know you'll have problems with after you fix the above mentioned ones. Notice how your function declarations above main do not match exactly with the error definitions after main? For example, GetDistance is declared at first as having 4 paramaters, but when you define it later you put in an extra parameter called double d. Also, for PrintResults you declare all parameters as ints but later on then at the bottom you change to double. When you declare a function, it must match exactly with how you define it later on, otherwise the compiler will view these as two different functions.

    Also, a couple of your functions are listed as returning variables, yet you don't return anything.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here is what the format of a function should look like:
    Code:
    #include <iostream>
    using namespace std;
    
    //function 'declaration'
    double My_func(double d1, double d2); 
    
    int main()
    {
    
    	double x1 = 10.0;
    	double x2 = 3.5;
    
    	double answer = My_func(x1, x2);  
    
    	cout<<answer<<endl;
    
    	return 0;
    }
    
    //function 'definition':
    double My_func(double d1, double d2)
    {
    	double result = d1 + d2;
    	return result;
    }
    You also might want to read this one page tutorial on functions:

    http://www.cprogramming.com/tutorial/lesson4.html

  8. #8
    High school loser
    Join Date
    Dec 2004
    Posts
    27
    New set of errors includes:
    N:\distancecode.cpp In function `int GetCoordinate()':
    7 N:\distancecode.cpp syntax error before `double'
    N:\distancecode.cpp In function `double GetDistance(double, double, double, double)':
    8 N:\distancecode.cpp syntax error before `void'
    N:\distancecode.cpp In function `void PrintResults(double, double, double, double, double)':
    10 N:\distancecode.cpp syntax error before `int'
    N:\distancecode.cpp In function `int main(int, char**)':
    10 N:\distancecode.cpp too few arguments to function `void PrintResults(double, double, double, double, double)'
    19 N:\distancecode.cpp at this point in file
    25 N:\distancecode.cpp syntax error before `{' token
    27 N:\distancecode.cpp syntax error before `>>' token
    30 N:\distancecode.cpp `double GetDistance' redeclared as different kind of symbol
    8 N:\distancecode.cpp previous declaration of `double GetDistance(double, double, double, double)'
    30 N:\distancecode.cpp confused by earlier errors, bailing out (This is my favorite)
    [Linker error] undefined reference to `GetDistance(double, double, double, double)'

    Even the compiler gave up on this...
    "Confused by previous errors. Bailing out." --Bloodshed DevC++ compiler

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You'll need to repost the new code.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A major point... (beyond the function not returning a value as you indicate it should and the extra ';' after the function):

    Code:
    int GetCoordinate ();
    
    int main(int argc, char *argv[])
    {
        double X1=0;
        double Y1=0;
        double X2=0;
        double Y2=0;
    
        GetCoordinate();
    
        ...
    
    }  
    
    int GetCoordinate ()
    {
        cout << "Enter the coordinates of two points (X1,X2),(Y1,Y2)" << endl;
        cin >>X1>>X2>>Y1>>Y2;
    }
    The variables in blue above are local to the function main. They have no scope (they don't exist) beyond that function. Your GetCoordinate function attempts to store information into the variables in red which do not exist in that function. For this to work you would either need to make the variables X1, X2, Y1, and Y2 global or you would need to pass them into the GetCoordinate function (pass-by-pointer or pass-by-reference) from main.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    High school loser
    Join Date
    Dec 2004
    Posts
    27

    Posting updated code

    Updated code:
    Code:
    using namespace std;
    int GetCoordinate ()
    double GetDistance (double X1, double Y1, double X2, double Y2, double d)
    void PrintResults (double d, double X1, double Y1, double X2, double Y2)
    
    int main(int argc, char *argv[])
    {
      double X1;
      double Y1;
      double X2;
      double Y2;
      double d;
      GetCoordinate();
      d = GetDistance ( X1, Y1, X2, Y2);
      PrintResults();
    system ("PAUSE");
      return 0;
    }  
    
    int GetCoordinate ();
        {
            cout << "Enter the coordinates of two points (X1,X2),(Y1,Y2)" << endl;
            cin >>X1>>X2>>Y1>>Y2;
        }
        
        double GetDistance (double X1, double Y1, double X2, double Y2, double d)*/;
        {
            double d = sqrt(pow((Y2-Y1), 2.0) + pow((X2-X1), 2.0));
        }
        
        void PrintResults (double d, int X1, int Y1, int X2, int Y2);
        {
            cout << "The distance between ("<<X1<<", "<<X2<<") and" << endl;
            cout<< "("<<Y1<<", "<<Y2<<") is "<<d<<"." << endl;
        }
    And here's our collection of errors:
    • 7 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before ` double'
    • C:\My Documents\Zoraa stuff\distancecode.cpp In function `double GetDistance(double, double, double, double, double)':
    • 8 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before ` void'
    • C:\My Documents\Zoraa stuff\distancecode.cpp In function `void PrintResults(double, double, double, double, double)':
    • 10 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before ` int'
    • C:\My Documents\Zoraa stuff\distancecode.cpp In function `int main(int, char**)':
    • 18 C:\My Documents\Zoraa stuff\distancecode.cpp cannot convert `double* ' to `double' for argument `1' to `double GetDistance(double, double,
    • 10 C:\My Documents\Zoraa stuff\distancecode.cpp too few arguments to function `void PrintResults(double, double, double, double, double)'
    • 19 C:\My Documents\Zoraa stuff\distancecode.cpp at this point in file
    • 25 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before `{' token
    • 27 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before `>> ' token
    • C:\My Documents\Zoraa stuff\distancecode.cpp In function `double GetDistance(double, double, double, double, double)':
    • 10 C:\My Documents\Zoraa stuff\distancecode.cpp too few arguments to function `void PrintResults(double, double, double, double, double)'
    • 19 C:\My Documents\Zoraa stuff\distancecode.cpp at this point in file
    • 25 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before `{' token
    • 27 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before `>> ' token
    • C:\My Documents\Zoraa stuff\distancecode.cpp In function `double GetDistance(double, double, double, double, double)':
    • 30 C:\My Documents\Zoraa stuff\distancecode.cpp redefinition of `double GetDistance(double, double, double, double, double)'
    • 8 C:\My Documents\Zoraa stuff\distancecode.cpp `double GetDistance(double, double, double, double, double)' previously defined here
    • 30 C:\My Documents\Zoraa stuff\distancecode.cpp redefinition of `double GetDistance(double, double, double, double, double)'
    • 8 C:\My Documents\Zoraa stuff\distancecode.cpp `double GetDistance(double, double, double, double, double)' previously defined here
    • 30 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before `*' token
    • 36 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before `{' token
    • 38 C:\My Documents\Zoraa stuff\distancecode.cpp syntax error before `<< ' token

    Sorry if there's any repeated errors.

    And if someone could remind me how to pass-by-reference I'd be much obliged. All I remember is something about an ampersand...
    "Confused by previous errors. Bailing out." --Bloodshed DevC++ compiler

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    And if someone could remind me how to pass-by-reference I'd be much obliged. All I remember is something about an ampersand...
    A pointer is just a variable that holds a memory address, and placing an ampersand before a variable name just uses the address of that variable. So you can pass &variable to a function that expects a pointer. It's a bit like "pointer literals".

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I have a better idea--don't pass pointers or references. In fact, don't pass anything. Forget about calling a function to get the data, and do it in main() instead.

    And if someone could remind me how to pass-by-reference I'd be much obliged. All I remember is something about an ampersand...
    That's all it takes--you just use an ampersand after the type, e.g.:

    (double& p1, double& p2)

    What you really need to do is to get one function to work. I suggest you write a little practice program that sends one value to a function, and then the function doubles the value and returns it, and then in main() you display the value. Once you get a function like that to work, you'll have a better idea of how to proceed with all the errors you have. The general idea in programming is to write one function at a time and test the function until you get it to work. Then, you write another function and test it. You don't write several functions and then run your program when you are all done. So, I suggest you start over, and write one function at a time, and get everything to work before adding functions.
    Last edited by 7stud; 03-01-2005 at 01:34 AM.

  14. #14
    High school loser
    Join Date
    Dec 2004
    Posts
    27
    Anything's worth a try.
    Hope Heather's not too offended that we chucked her code and started over. It would help if she showed up to class once in a while though.
    Thanks guys.
    Although for future reference, I always seem to get errors like "Syntax error before <<" when I can't see anything wrong. Any suggestions?
    "Confused by previous errors. Bailing out." --Bloodshed DevC++ compiler

  15. #15
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    For the Syntax errors you might have spelled something wrong, or forgot a character. I do that a lot of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  3. What to make?
    By Caldus in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2005, 01:12 PM
  4. Errors in code
    By itzme in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2005, 05:11 PM
  5. unknown errors (to me at least)
    By bcraig in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 10:19 PM