-1

I am writing a code to declare a member function of a class as a friend, but I am getting errors. The code is

#include<iostream>

class Vect;

class Coordinate {
    float x;
    float y;

public:
    Coordinate(float a,float b):x(a),y(b){}// constructor
    Coordinate():x(0),y(0){};//constructor
    display()
    {
        std::cout<<"\nx:"<<x<<" "<<"y:"<<y;
    }
    friend Vect::add(Coordinate B);
};

class Vect {
public:
    add(Coordinate A)
    {
        std::cout<<A.x;
    }
};

The exact errors are

invalid use of incomplete type 'class Vect'| and forward declaration of 'class Vect'|

FrankS101
  • 2,112
  • 6
  • 26
  • 40
  • 3
    Please have a bash at formatting the code to make it readable – Ed Heal Jan 20 '18 at 11:00
  • 2
    You cannot declare a function without a return type in ISO C++. – tambre Jan 20 '18 at 11:01
  • 3
    `add(Coordinate A)` is not a valid function declaration, you are missing a return type – UnholySheep Jan 20 '18 at 11:02
  • 2
    And get it to compile i.e. first error is `prog.cpp:10:9: error: ISO C++ forbids declaration of ‘display’ with no type [-fpermissive] display()` – Ed Heal Jan 20 '18 at 11:02
  • The compiler complains about class `Vec` being incomplete... so make it complete. – user202729 Jan 20 '18 at 11:05
  • 2
    @user9212993 [You can](https://tio.run/##dc3BCgIhFIXhvU8htBkX9QAqgfUO7e1eJy44KqNGMMyz2wwTFEXbw893IKX9DaC1HQXwFZ2mmMvo7HBk4G3O/BzjiBRsceq1XByUiaV69QSScX6PhNwidu@UG6HYrH6Iacl7H23hD/Uh9CO5gBu06lJ@caeN@x8YsdK5oJQQa9HaHJaHmQ2WQiemubUn). – user202729 Jan 20 '18 at 11:07
  • @user202729 Must have been an old constraint in c++03. Thanks for the info. –  Jan 20 '18 at 11:09
  • I am using c11 so other functions without a return type(even void) are compiling without problems. –  Jan 20 '18 at 11:10
  • @user9212993 [You can in C++03](https://tio.run/##dc3dCsIgFMDxe59C6GZjFEF3asHqHboddnRDcCpTIxh79cyxoC@6ODfn/PkdcG7dAaS0UgZ0FJIp68MgeX9AoLn3@GTtIJThQdLn5iwhjMjFi1ZAEMZXqwTmQhSvFNclRRP9Icact9rygG/0TWgHJY1YoFkn5Is7Ltz/oC5n2gdBCNgYGKs3@cOEeq5M8XnDjOGmAaejnydXKd2h1bzzaZ2rPVTVdvcA). – user202729 Jan 20 '18 at 11:10
  • 2
    @user8865657 C != C++. – user202729 Jan 20 '18 at 11:10
  • This code would compile even less in C11 than in C++11 - it's invalid in both language standards – UnholySheep Jan 20 '18 at 11:14
  • Most likely some compiler extension. Learn standard C++ please. And yes, C doesn't have `` header. – user202729 Jan 20 '18 at 11:15
  • sorry i meant c++11 –  Jan 20 '18 at 11:35
  • 1
    Why are you using `friend` in the first place? – Ed Heal Jan 20 '18 at 11:50

2 Answers2

2
  class Y; //forward declaration of class Y
  class X
  {
  public:
        int getmark(Y);
  };

  class Y
  {
  int mark;
   public:
   Y() {}
   friend int X::getmark(Y);
   };

   int X::getmark(Y obj) {
   cin >> obj.mark;
  return obj.mark;
  }

int main()
{
  X a;
  Y b;
  a.getmark(b);
}

At first, when the object a (class X) is created, a forward declaration of class Y is necessary in order to declare the Y argument to X::getmark().

Creating object b (class Y) wont be a problem as the compiler knows class X exists (for the friend function).

Then, simply call the function getmark() through the object a.

Note: It is necessary to declare the function getmark() after the declaration of class Y, or else compiler will consider the class Y as an incomplete type.

1

you can use this :

class Vect;
class Coordinate
{
 public:
  Coordinate(float a = 0f, float b = 0f) :x(a), y(b) {}// constructor
  float x;
  float y;
void display()
{
    std::cout << "\nx:" << x << " " << "y:" << y;
}
  friend int add(Coordinate B);
};
class Vect
{
  friend int add(Coordinate B);
public:

};

int add(Coordinate B)
{
   std::cout << B.x << std::endl;
   return B.x;
}