Thread: Error with friend method

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Error with friend method

    I was compiling an old programming assignment about manipulating polynomials represented as linked list of its terms and I ran into a plethora of unfamiliar errors. I originally used Visual Studio Express to do the assignment, where it successfully compiled and ran, if I remember correctly. Recently, however, I installed Ubuntu on an old Thinkpad and decided to give gcc/g++ a test run. Unfortunately, it failed to compile and gave me some strange errors on two lines where I declare a method a friend of the Term class. Here are the errors and relevant part of Poly.h.

    In file included from assign7.cpp:9:
    Poly.h:20: error: expected ‘,’ or ‘...’ before ‘&’ token
    Poly.h:20: error: ISO C++ forbids declaration of ‘Poly’ with no type
    Poly.h:21: error: expected ‘,’ or ‘...’ before ‘&’ token
    Poly.h:21: error: ISO C++ forbids declaration of ‘Poly’ with no type

    Code:
    /*
    
    
    [Removed personal info]
    
    
    */
    
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <math.h>
    using namespace std;
    
    /* A class representing a term of a polynomial as a node in a linked list. Contains value of coefficient, 
    degree, and a pointer to the next list */
    class Term
    {
    	friend class Poly;
    	friend ostream& operator<<(ostream& out, const Poly& p);  //Line 20
    	friend istream& operator>>(istream& in, const Poly& p);       //Line 21
    public:
    	Term(float c, int d) { coefficient = c; degree = d; next_term = 0; }
    	Term() { coefficient = 0; degree = 0; next_term = 0; }
    	const float evaluate(float x) const { return ( coefficient * pow(x,degree) ); }
    	const Term operator+(const Term& b) const;
    	const Term operator*(const Term& b) const;
    	const bool operator==(const Term& b) const;
    	const bool operator!=(const Term& b) const;
    private:
    	float coefficient;
    	int degree;
    	Term *next_term;
    };

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Where is the class Poly defined? Is it before Term? If it's after, you might need to either have Poly appear first, or just tell the compiler it's coming:
    Code:
    class Poly;
    ...somewhere before Term.

    I'm not sure if the "friend class Poly" is significant enough to count... perhaps it was for MSVC++...
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    That did the trick, thanks a lot.

    So should one normally declare/prototype classes like one does with methods?

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    A class may be declared 'friend' without its (forward) declaration, but when you use it as a function argument, it may not. Note that the 'friend' declaration tells something about the class "Term", not about the class 'Poly'.

    This is exactly what the second message for both lines is telling you:
    ISO C++ forbids declaration of ‘Poly’ with no type
    it does not recognize "Poly" as a type but as a variable.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Oh, alright, that helps. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  3. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  4. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 10:53 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM