-1

While creating my C++ project I found out some errors concerning my syntax but some other files and here is the errors I found:

C:\Users\user\Desktop\SUJET 1\EMPLOYE.o EMPLOYE.cpp:(.rdata$.refptr._ZN7EMPLOYE6valeurE[.refptr._ZN7EMPLOYE6valeurE]+0x0): undefined reference to `EMPLOYE::valeur'.

C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib\libmingw32.a(lib64_libmingw32_a-crt0_c.o) In function `main':

C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crt0_c.c undefined reference to `WinMain'

C:\Users\user\Desktop\SUJET 1\collect2.exe [Error] ld returned 1 exit status

C:\Users\user\Desktop\SUJET 1\Makefile.win recipe for target '"SUJET' failed

files:

EMPLOYE.cpp:

#include <iostream>
#include <string>
#include"EMPLOYE.h"

using namespace std;

EMPLOYE::EMPLOYE(){};
EMPLOYE::EMPLOYE(string n ,int m ,int i){
    nom=n;
    matricule=m;
    indice=i;
    int valeur = 10;
};

void EMPLOYE :: afficherEmploye(){
    cout << "votre nom est"<<nom<<"votre matricule est" << matricule << "votre indice est" << indice << endl;   
};

int EMPLOYE :: salaire(){
    return indice*valeur; //salaire
};

EMPLOYE.h:

#ifndef EMPLOYE_h
#define EMPLOYE_h
#include <iostream>
#include <string>

using namespace std;

class EMPLOYE{
protected:
    int indice;
    string nom;
    static int valeur ;

public:
    int matricule;
    EMPLOYE();
    EMPLOYE(string , int, int);
    void afficherEmploye();
    virtual int salaire();
};

#endif 
Community
  • 1
  • 1
  • 1
    Please copy-paste error messages into the question, makes it easier to read. The third error is undefined reference to WinMain. Does your program contain a `main` function? – Lukas-T Nov 20 '19 at 14:58
  • 1
    Also you should read about [Why using namespace std is considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). This can become a real problem when used in a header. – Lukas-T Nov 20 '19 at 15:01
  • i edited the errors part @churill then should i try to use std:: instead ? – Haytem Talbi Nov 20 '19 at 15:04
  • 1
    Thanks :) Using `std::` is safer. In case the names get too long you can also use [namespace aliasing](https://en.cppreference.com/w/cpp/language/namespace_alias) or [type aliasing](https://en.cppreference.com/w/cpp/language/type_alias) to shorten them. – Lukas-T Nov 20 '19 at 15:12
  • The errors point to the fact that you're missing the function called `main`. This function needs to be a part of all c++ programs. – Rokas Višinskas Nov 20 '19 at 15:14

2 Answers2

1

You need to initialize the static variable explicitly. https://www.geeksforgeeks.org/static-keyword-cpp/

#include <iostream>
#include <string>
#include"doc.h"

using namespace std;

int EMPLOYE::valeur = 0;

EMPLOYE::EMPLOYE(){};
EMPLOYE::EMPLOYE(string n ,int m ,int i){
nom=n;
matricule=m;
indice=i;
valeur = 10;
};
void EMPLOYE :: afficherEmploye(){
cout<<"votre nom est"<<nom<<"votre matricule est"<<matricule<<"votre indice 
est"<<indice<<endl;
};

int EMPLOYE :: salaire(){
return indice*(EMPLOYE::valeur); //salaire
};

int main()
{
//do something
}
John smith
  • 160
  • 6
  • Please also state it in words, not just code. Your example implies adding the main function to the `employe.cpp` file which defeats the purpose of splitting a class into a `.h` and `.cpp`. – Rokas Višinskas Nov 20 '19 at 15:21
  • Are you referring to your own link? If so, it says nothing about the fact that you must have a `main` function. And the purpose of a stack overflow answer is to ANSWER the question completely, without leaving uncertainties out. In no way am I telling you to code it for them. But that's actually what you've done. I'm asking you to explain, not just show the finished product. – Rokas Višinskas Nov 20 '19 at 15:29
-1

You're compiling a program without an entry point aka a main function

int main(int argc, char** argv){ return 0; }

It is required as it will be the entry point of your program, if you're building a library you might want to change your compiler settings. And of course, if you want things to happen, put something else inside the main, you can discard the parameters is you don't use them

returnVoid
  • 87
  • 5