1

in this program, I am interested by this problem: how to access to the variable building and I do not understand errors:

gcc struct1En.cc -lstdc++ -o struct1En 
struct1En.cc: In member function 'void job::cie::printBuilding()':
struct1En.cc:22: error: 'job' is not a base of 'job::cie'
struct1En.cc: In function 'int main()':
struct1En.cc:48: error: incompatible types in assignment of 'const char [10]' to 'char [20]'

I read people use class rather than struct. But in this case I just want to know why I can not reach the variable building and why the error say incompatible types in assignment of 'const char[10] to 'char [20]'

#include <iostream>

struct job {
    private:
    char name[20];
    double salary;

    public:
    int building;
    void initName() {std::cin >> name;}
    void initSalary() {std::cin >> salary;}
    void printName() {std::cout << "name: " << name << std::endl;}
    void printSalary() {std::cout << "Salary: " << salary << std::endl;}
    void printBuilding() {std::cout << "Building number : " << building << std::endl;}

    struct cie {
        char name[20];
        int employee;

        void printName(){ std::cout << "name: " << cie::name << std::endl;}
        void printEmployee() {std::cout << "number of employees : " << employee << std::endl;}
        void printBuilding() {std::cout << "Building number: " << this->job::building << std::endl;}
    };

};


int main() {

    job teacher;
    std::cout << "Enter a name: \n";
    std::cout << "Name: "; 
    teacher.initName();
    teacher.printName();

    std::cout << "Enter a salary: \n";
    std::cout << "Salary: ";
    teacher.initSalary();
    teacher.printSalary();

    std::cout << "Enter the number of the building: \n";
    std::cout << "Building n°: ";
    std::cin >> teacher.building;
    teacher.printBuilding();


    job::cie educationNationale;
    educationNationale.name = "Courriere";
    educationNationale.printName();

    educationNationale.employee = 200 + 1;
    educationNationale.printEmployee();

    educationNationale.printBuilding();

    return 0;
}
ripeisripe
  • 69
  • 8
  • 1
    A struct within a struct is a seperate struct. It doesn't get any information from the parent structure, nor does it gain access to any of its variables. – David Ledger Sep 20 '18 at 10:10
  • Ah ok thank you. I change and now this error disappear at the compilation. void printBuilding(job aJob) {std::cout << "Building number: " << aJob.building << std::endl;} and educationNationale.printBuilding(teacher); sorry for this duplicate. – ripeisripe Sep 20 '18 at 10:14
  • I still do not understand this error: incompatible types in assignment of 'const char [10]' to 'char [20]' Why the compilator say const? – ripeisripe Sep 20 '18 at 10:15
  • I find a start for solution in this question: https://stackoverflow.com/questions/5414186/errorincompatible-types-in-assignment-of-const-char5-to-char10 – ripeisripe Sep 20 '18 at 10:22
  • So I change the code: void initName() {std::cin >> name;} (in struct cie) and std::cout << "Enter a name: \n"; std::cout << "Name: "; educationNationale.initName(); educationNationale.printName(); – ripeisripe Sep 20 '18 at 10:30
  • 1
    When you assign educationNationale.name = "Courriere"; The name variable is a char[20] and the "Courriere" is a char[10]. These are seperate types. You likely want to use a std::string where you have char[20]. Type include the header #include and replace all occurances of char[20] in your classes (the name members) with std::string. – David Ledger Sep 21 '18 at 02:57
  • 1
    if you do need to use char[20] you can use, memcpy but this is likely inadvisable as mistakes with it can be costly. This code works: char label[20]; memcpy(label, "Courriere", sizeof("Courriere")); But is unsafe as if you attempted to assign a name that was longer than the char[20] you would write over sections of memory that are outside of the class (BAD). – David Ledger Sep 21 '18 at 02:58
  • Thank you for explanation! I use std::string and no memcpy as it is likely inadvisable. – ripeisripe Sep 21 '18 at 07:05

0 Answers0