How to use Class Prototype in C++

I'm trying to create class Object before defining class. Here is the example.

class A;
class B;

class A{
    public: 
    void fun(B obj){
    }
};
class B{ };
int main(){

    return 0;
}

Here is the Error List:

In member function 'void A::fun(B)':
6   13  [Error] 'obj' has incomplete type
2   7   [Error] forward declaration of 'class B'

Any Solution? I already Searched about it, but failed to solve this.. Thanks in Advance.

Answers


Provide A::fun()'s definition out-of-line, after B has been fully declared :

#include <iostream>

class A;
class B;

class A {
    public: 
    void fun(B obj);
};

class B{ };

void A::fun(B obj) {}

int main() {
    return 0;
}

The definition wil typically go into a .cpp file, where you'd include B.h to have its complete declaration.

Also, please don't using namespace std;.


Need Your Help

Q.Promise: How to throw exception in callback correctly?

javascript q

My simple code here http://jsfiddle.net/xh6960fo/

Display my account navigation links

magento magento-1.7

I have created custom page and added that link to my account page. But if I click on that link side bar disappears. How can I show my account navigation sidebar in my custom module page?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.