lundi 7 juin 2021

Using friend function corrrectly across multiple Classes in C++

I am trying use a friend function. The function should be a friend to all the classes that i have. But i get multiple errors some of which says incomplete type. The following are files that i have:

main.cpp

#include <iostream>
#include "my_ClassA.h"
#include "my_ClassB.h"
#include "my_ClassC.h"
#include "my_ClassD.h"


int main()
{
    std::cout<<"Hello World";
    my_namespace::my_ClassA object1;//this will do some computation and set everything up for object1
    my_namespace::my_ClassB object2(object1);//this object2 will use object1 to further do other computation
    my_namespace::my_ClassC object3(object2);
    
    my_namespace::my_classD object4(object4);    
    runComputation(object1&, object2&, object3&, object4&);

    return 0;
}

my_ClassA.h

#pragma once 
#include<string>
#include <vector>

//these three includes are for friend function BUT result in error incomplete type
 #include "my_ClassB.h"
 #include "my_ClassC.h"
 #include "my_ClassD.h"
/////////////////////////////////////////////
namespace my_namespace{
    class my_ClassA{
        friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
            
        
        private:
           std::string name;  
           std::vector<std::string> vec;
          public:
            std::vector<std::string> get_vec();
    };
    void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
    
}

my_ClassA.cpp

#include "my_ClassA.h"

std::vector<std::string> my_namespace::my_ClassA::get_vec(){
    return vec;
}

my_ClassB.h

#pragma once 
#include<string>
#include <vector>

#include "my_ClassA.h"
#include "my_ClassC.h"
#include "my_ClassD.h"
namespace my_namespace{
    class my_ClassB{
        friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
        private:
           std::string name;  
           std::vector<std::string> vec;
          public:
            std::vector<std::string> get_vec();
            my_ClassB(my_ClassA);
            my_ClassB(){
                ;
            }
    };
}

my_ClassB.cpp

#include "my_ClassB.h"

std::vector<std::string> my_namespace::my_ClassB::get_vec(){
    return vec;
}
my_namespace::my_ClassB::my_ClassB(my_ClassA temp_objA){
    ;
}

my_ClassC.h

#pragma once 
#include<string>
#include <vector>
#include "my_ClassB.h"
#include "my_ClassA.h"
 #include "my_ClassD.h"
namespace my_namespace{
    class my_ClassC{
        friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
        private:
           std::string name;  
           std::vector<std::string> vec;
           my_ClassB objB;
          public:
            my_ClassC(my_ClassB);
            std::vector<std::string> get_vec();
    };
}

my_ClassC.cpp

#include "my_ClassC.h"
#include "my_ClassB.h"
std::vector<std::string> my_namespace::my_ClassC::get_vec(){
    return vec;
}
my_namespace::my_ClassC::my_ClassC(my_ClassB temp_objB){
    ;
}

my_ClassD.h

#pragma once 
#include<string>
#include <vector>
#include "my_ClassA.h"
#include "my_ClassB.h"
 #include "my_ClassC.h"

namespace my_namespace{
    class my_ClassD{
        friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
        private:
           std::string name;  
           std::vector<std::string> vec;
           my_ClassA objA;
          public:
            std::vector<std::string> get_vec();
    };
}

my_ClassD.cpp

#include "my_ClassD.h"

std::vector<std::string> my_namespace::my_ClassD::get_vec(){
    return vec;
}

I tried using the getters of each of the classes in the main.cpp. But some my classes have large size vectors,sets etc etc. So i do not want to copy them again and again. So instead i want to access the data members of the classes directly in a function called runComputation. And that function will be taking the object created as references so that copy doesn't happen inside main.cpp.

What i have is this: First i create different objects which may take the previously created object as input in main.cpp. After all the objects are created successfully, i want to run some computation on those objects. Now the problem is that i can use the getters and setters of the objects created in the main.cpp file. But the objects have large vectors and other objects inside them, and so they will be copied each time i use them in a for loop using getters. To avoid this i want to create a friend function that can take these objects as references and avoid copying. How can i resolve this ? And is there any better way of achieving this?

PS: I am aware of ADL. Will ADL be used when i write a friend declaration like friend void runComputation(someobject&); and then after the class to make this friend function visible void runComputation(someobject&);

Aucun commentaire:

Enregistrer un commentaire