0

I will write the code of the header file and two .cpp files I created under the same folder below. And I tried this under Visual Studio, gcc-gnu, and dev c++. I am getting the following error in the file employe.cpp:

expected initializer before '.' token

employee.h:

#ifndef employee
#define employee
#include <iostream>
using namespace std;
class employe{
public:
        string name;
        int id;
        int salary;
        void showInfos();
};
#endif 

employe.cpp:

#include "employee.h"
#include <iostream>
using namespace std;

void employe.showInfos(){
    cout<<"Ad:"<<employe.name<<endl<<"Id:"<<employee.id<<endl<<"Salary:"<<employee.salary;
}

main.cpp:

#include <iostream>
#include "employee.h"
#include <iostream>

int main(){
    
    employe.id=21;
    cout<<employe.id;
    
    return 0;
}

main.cpp errors:

expected unqualified-id before '.' token
expected primary-expression before '.' token

I meant to create my own header file and use it. But this happened.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Voting to close as a typo. You need `::`, not `.` when defining the function. `void employe.showInfos(){` -> `void employe::showInfos(){` – NathanOliver Dec 01 '22 at 21:19
  • now it gives this error : expected primary-expression before '.' token – muratkavak Dec 01 '22 at 21:26
  • ... and then, *inside* the member function definition, just use the `name` and others. No need for the `employee.`. – Adrian Mole Dec 01 '22 at 21:28
  • 1
    On a side note: [*never* use `using namespace std;` in a header file](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) (or at all, if you can help it). – Remy Lebeau Dec 01 '22 at 21:29
  • `employe` is the name of a type, not a variable, so `employe.id = 21;` is not valid. Try something like `employe bob; bob.id = 21;` Also employee.h doesn't need `#include `, but does need `#include ` – Avi Berger Dec 01 '22 at 21:30
  • ı had to for string define – muratkavak Dec 01 '22 at 21:30
  • 1
    Macros should be named in all caps. – HolyBlackCat Dec 01 '22 at 21:38

1 Answers1

1

employe.cpp fails because you are qualifying everything incorrectly. It needs to look more like this:

#include "employee.h"
#include <iostream>
using namespace std;

void employe::showInfos(){
    cout << "Ad:" << name << endl << "Id:" << id << endl << "Salary:" << salary;
}

main.cpp fails because none of your struct members are static so you can't access any of them on the struct type itself, like you are trying to do. You need to declare an object instance of employe, eg:

#include "employee.h"

int main(){
    
    employe emp;

    emp.name = "Joe";
    emp.id = 21;
    emp.salary = 12345;
    emp.showInfos();
    
    return 0;
}

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • it helped a lot thank you but now it gives this erros : undefined reference to `WinMain' and ıd returned 1 exit status – muratkavak Dec 01 '22 at 21:35
  • @muratkavak works fine for me: https://onlinegdb.com/REo4yCe3f There are plenty of other questions on StackOverflow for that specific error. It means your project is not configured correctly, so you need to fix it. That has nothing to do with the code shown. – Remy Lebeau Dec 01 '22 at 21:38
  • ım using DevC++ as compiler. could this be the problem? what do you suggest as compiler ? – muratkavak Dec 01 '22 at 21:42
  • @muratkavak The way one starts with a new toolchain is to write a "HelloWorld" to make sure you can compile something. – 463035818_is_not_an_ai Dec 01 '22 at 22:21
  • The compiler is not the issue. The linker is. It can't find the code for the expected entrypoint for the project. This is because the project is configured as a Windows GUI project (entrypoint `WinMain()`), but the code shown is written for a Console project instead (entrypoint `main()`). – Remy Lebeau Dec 01 '22 at 22:23