Thread: please help me debug

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    please help me debug

    Here is a list of the errors and the code afterward.
    If we can figure out the first errors the other error is just the same thing.

    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    vector.cpp:
    Error E2061 vector.cpp 36: Friends must be functions or classes
    Error E2139 vector.cpp 36: Declaration missing ;
    Error E2321 vector.cpp 37: Declaration does not specify a tag or an identifier
    Error E2061 vector.cpp 37: Friends must be functions or classes
    Error E2139 vector.cpp 37: Declaration missing ;
    Error E2321 vector.cpp 38: Declaration does not specify a tag or an identifier
    *** 6 errors in Compile ***
    Code:
    //vector.cpp example 10.11 pg 446 C++ primer plus
    #ifndef _VECTOR_CPP_
    #define _VECTOR_CPP_
    
    class Vector
    {
    	private:
    		double x;
    		double y;
    		double mag;			//magnitude, length of vector
    		double ang;			//direction of vector
    		char mode;			// 'r'=rectangular, 'p'=polar
    		// private methods for setting values
    		void set_mag();
    		void set_ang();
    		void set_x();
    		void set_y();
    
    	public:
    		Vector();
    		Vector(double n1, double n2, char form='r');
    		void set(double n1,double n2, char form='r');
    		~Vector();
    		double xval() const {return x;}				// inline return x val
    		double yval() const {return y;}
    		double magval() const {return mag;}
    		double angval() const {return ang;}
    		void polar_mode();
    		void rect_mode();
    		// operator overloading
    		Vector operator+(const Vector & b) const;
    		Vector operator-(const Vector & b) const;
    		Vector operator-() const;
    		Vector operator*(double n) const;
    		// friends
    		friend vector operator*(double n, const Vector & a);    // errors start here
    		friend ostream & operator<<(ostream & os,const Vector & v);
    };
    #endif

  2. #2
    Unregistered
    Guest
    can you post the definitions of the friend functions? They might be the cause...

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    here is an exerpt

    Here is the declaration of the friend functions taken from a larger file.
    Code:
    // friend methods
    // multply n by vector a
    Vector operator*(double n, const Vector & a)
    {
    	return a*n;
    }
    
    //display rectangular coordinates if mode is 'r',
    //else display polar coordinates if mode is 'p'
    
    ostream & operator<<(ostream & os, const Vector & v)
    {
    	if (v.mode=='r')
    		os << "(x,y) = (" <<v.x << "," << v.y<<")";
    	else if (v.mode=='p')
    	{
    		os << "(m,a) = ("<<v.mag << ","<<v.ang*Rad_to_deg<<")";
    	}
    	else
    		os << "Vector object mode is invalid";
    	return os;
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I have narrowed it down to three errors:
    C:\Documents and Settings\rippascal\src>bcc32 vector.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    vector.cpp:
    Error E2061 vector.cpp 37: Friends must be functions or classes
    Error E2139 vector.cpp 37: Declaration missing ;
    Error E2321 vector.cpp 38: Declaration does not specify a tag or an identifier
    *** 3 errors in Compile ***
    Code:
    //vector.cpp example 10.11 pg 446 C++ primer plus
    #ifndef _VECTOR_CPP_
    #define _VECTOR_CPP_
    
    class Vector
    {
    	private:
    		double x;
    		double y;
    		double mag;			//magnitude, length of vector
    		double ang;			//direction of vector
    		char mode;			// 'r'=rectangular, 'p'=polar
    		// private methods for setting values
    		void set_mag();
    		void set_ang();
    		void set_x();
    		void set_y();
    
    	public:
    		Vector();
    		Vector(double n1, double n2, char form='r');
    		void set(double n1,double n2, char form='r');
    		~Vector();
    		double xval() const {return x;}				// inline return x val
    		double yval() const {return y;}
    		double magval() const {return mag;}
    		double angval() const {return ang;}
    		void polar_mode();
    		void rect_mode();
    		// operator overloading
    		Vector operator+(const Vector & b) const;
    		Vector operator-(const Vector & b) const;
    		Vector operator-() const;
    		Vector operator*(double n) const;
    		// friends
    		friend Vector operator*(double n, const Vector & a);// here was one error had to capitoliz vector
    		friend ostream & operator<<(ostream & os,const Vector & v);// last remaing error here
    
    };
    #endif

  5. #5
    Unregistered
    Guest
    sorry, still cant see it. I would check through either side of the friend function definitions, because the missing ; error can sometimes occur in the wrong files, eg when you miss it off from a class declaration.

    Also try looking at what you have changed to get rid of the first set of errors, they look like the same problem in 2 places - one of which you have eradicated somehow.

    Good luck.....

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    The compiler that comes with dev c++ gave me different error messages that allowed me to fix the first error.

    Executing g++.exe...
    g++.exe "C:\Documents and Settings\rippascal\src\vector.cpp" -o "C:\Documents and Settings\rippascal\src\vector.exe" -s -I"C:\Dev-C++\include" -I"C:\Dev-C++\include\g++-3" -I"C:\Dev-C++\include" -L"C:\Dev-C++\lib"
    Execution terminated
    C:\Documents and Settings\rippascal\src\vector.cpp:37: ANSI C++ forbids declaration `ostream' with no type
    C:\Documents and Settings\rippascal\src\vector.cpp:37: `ostream' is neither function nor method; cannot be declared friend
    C:\Documents and Settings\rippascal\src\vector.cpp:37: parse error before `&'
    0

    This code is directly from the book. The problem I am having is the main thrust of this chapter overloading (in this case the ostream oprator cout and >>) and the use of friend functions. I don't want to move on w/o understanding how to do this and get it working.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I changed the extension of my header file from cpp to hpp and now I get only one error that is differnt than before.

    C:\Documents and Settings\rippascal\src>bcc32 randwalk.cpp vectorf.cpp vector.hp
    p
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    randwalk.cpp:
    vectorf.cpp:
    vector.hpp:
    Error E2141 vector.hpp 5: Declaration syntax error
    *** 1 errors in Compile ***

    Here is the code again
    Code:
    //vector.cpp example 10.11 pg 446 C++ primer plus
    #ifndef _VECTOR_HPP_
    #define _VECTOR_HPP_
    
    class Vector  // syntax error here ????
    {
    	private:
    		double x;
    		double y;
    		double mag;			//magnitude, length of vector
    		double ang;			//direction of vector
    		char mode;			// 'r'=rectangular, 'p'=polar
    		// private methods for setting values
    		void set_mag();
    		void set_ang();
    		void set_x();
    		void set_y();
    
    	public:
    		Vector();
    		Vector(double n1, double n2, char form='r');
    		void set(double n1,double n2, char form='r');
    		~Vector();
    		double xval() const {return x;}				// inline return x val
    		double yval() const {return y;}
    		double magval() const {return mag;}
    		double angval() const {return ang;}
    		void polar_mode();
    		void rect_mode();
    		// operator overloading
    		Vector operator+(const Vector & b) const;
    		Vector operator-(const Vector & b) const;
    		Vector operator-() const;
    		Vector operator*(double n) const;
    		// friends
    		friend Vector operator*(double n, const Vector & a);
    		friend ostream & operator<<(ostream & os,const Vector & v);
    };
    #endif

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I got the program to compile in Dev C++ but I still don't understand why it doesnt work using the borland campiler maybe it has somthing to do with the order I listed the files when I told it to compile them.

    Still a newbie bit at least I can move on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM