0

I started to learn some oop and I have a question, why I can't put the function in first class, I know there is a way to write the friend method down under second class. If I put it in the first class where it belongs my compiler shows the error C2027: "Error C2027 use of undefined type 'Calculator'"

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <ctime>

using namespace std;   

class Calculator;

class PlacadeBaza {
    char denumire_procesor[100];

public:    
    char* getDenumire() {
        return denumire_procesor;
    }

    void set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]) { //C2027
        a.memorie_RAM = memorie; 
        strcpy(b.denumire_procesor, denumire); //C2027
    }
};

class Calculator {
    int memorie_RAM;

public:     
    int getMemorie_RAM() {
        return memorie_RAM;
    }
    
    friend void PlacadeBaza::set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]);
};

int main() {
    Calculator a;
    PlacadeBaza b;
    int memorie;
    char denumire[100];
    
    cin >> memorie >> denumire;

    b.set(a, b, memorie, denumire);

    cout << a.getMemorie_RAM();
    cout << b.getDenumire();
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    To use an object of a class, you need the full and complete definition of the class. Otherwise the compiler will not know what possible functions it might have. Any decent [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), tutorial or class should have taught you how to define (implement) member function outside of the class definition. That way you can define the classes, and then define the functions. – Some programmer dude Sep 10 '22 at 18:43
  • 1
    The function doesn't make sense the way it's written: `a` is a copy that gets destroyed on the function exit of `PlacadeBaza::set`. the new value is never read. The same is true for `b`. You could define the `PlacadeBaza::set` outside the class after the class declaration of `Calculator` though. (May need to be marked as `inline` in order to preserve the "inlinedness"). – fabian Sep 10 '22 at 18:47
  • Aside: Why not use `std::string`? – Chris Sep 10 '22 at 18:58
  • *"why I can't put the function in first class"* -- because that would use 'Calculator' while 'Calculator' is an undefined type. Which your compiler already told you. If you want more explanation than your compiler gave, maybe you should add more explanation to your question? Perhaps argue why your compiler is wrong. – JaMiT Sep 10 '22 at 19:56
  • "*If I put it in the first class where it belongs"* -- I think "it" refers to the definition of the friend method, `set()`? If my interpretation is accurate: Why do you believe that definition belongs inside the class definition? Definitions of member functions are often located outside the class definition. – JaMiT Sep 17 '22 at 08:24

1 Answers1

-1

Setting aside other issues with the code, if you wish PlacadeBaza to be able to access private members of Calculator you need to declare that PlacadeBaza is a friend class of Calculator.

#include <iostream>
#include <ctime>

using namespace std;
   
class PlacadeBaza;

class Calculator {
    int memorie_RAM;

public:     
    int getMemorie_RAM() {
        return memorie_RAM;
    }

    friend class PlacadeBaza;
};

class PlacadeBaza {
    char denumire_procesor[100];

public:    
    char* getDenumire() {
        return denumire_procesor;
    }

    void set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]) { //C2027
        a.memorie_RAM = memorie; 
        strcpy(b.denumire_procesor, denumire); //C2027
    }
};

int main() {
    Calculator a;
    PlacadeBaza b;
    int memorie;
    char denumire[100];
    
    cin >> memorie >> denumire;

    b.set(a, b, memorie, denumire);

    cout << a.getMemorie_RAM();
    cout << b.getDenumire();
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • *"Setting aside other issues with the code,"* -- but one of the "other issues" is the subject of the question (asking why `PlacadeBaza::set()` is unable to access elements of `Calculator` even though the function is a friend of the class). You've side-stepped the question that was asked by taking a different approach. – JaMiT Sep 17 '22 at 08:18
  • *"if you wish `PlacadeBaza` to be able to access private members of `Calculator`"* -- big "if". The question does not indicate that all of `PlacadeBaza` is intended to access private members. According to the question, only the `set()` method is supposed to have that ability (based upon the question's `friend` declaration and the lack of other access attempts). – JaMiT Sep 17 '22 at 08:21