Hi i'm trying to fill Class members in C++ so i put the class definitions in a header file nomprenom.h but it shows an error "undefined reference to function()" what is wrong in my code?
the purpose of the code is to enter name and second name in the private class "nomprenom" using member functions
nomprenom.h CODE:
#ifndef NOMPRENOM_H
#define NOMPRENOM_H
class nomprenom{
char *nom;
char *prenom;
public:
void dispnom();
void dispprenom();
void setn(char *nom,char *prenom);
};
void dispnom();
void dispprenom();
void setn(char*,char*);
#endif
and i defined member functions in a cpp file nomprenom.cpp
nomprenom.cpp CODE:
#include <iostream>
#include "nomprenom.h"
void nomprenom::setn(char *name1,char *secname1)
{
nom = name1;
prenom = secname1;
}
void nomprenom::dispnom()
{
printf("%s",nom);
}
void nomprenom::dispprenom()
{
printf("%s",prenom);
}
so when i call functions in main.cpp it shows a compilation error: "undefined reference to setn(char*,char*) dispnom() dispprenom() "
main.cpp CODE:
#include <iostream>
#include "nomprenom.h"
int main(int argc, char** argv) {
char nom[20],prenom[20];
printf("enter name and second name:");
scanf("%s %s",&nom,&prenom);
setn(nom,prenom);
dispnom();
dispprenom();
return 0;
}
Remove these non member functions.
void dispnom();
void dispprenom();
void setn(char*,char*);
and you are not using anything from a class. Keep this thing in mind to call a member function, you need an object of that class.
nomprenom obj1;
obj1.setn(nom, prenom);
obj1.dispnom();
obj1.dispprenom();
It makes sense, that you wanna save these record for this object and display this object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.