Sep 6, 2016 at 4:39am UTC
hey every body.
I keep getting this error message when I compile my code:
UserClass.cpp:78:32: error: unexpected type name 'string': expected expression
MyUser myFname = new MyUser(string& myData);
file1.cpp that I called myUser.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <fstream>
#include <string>
#include "MyUser.h"
int addUser(int );
void listSaved(int );
using namespace std;
struct users{
string fname;
string lname;
string phone;
};
int addUser(int compt){
if (compt == 10){
cout<<"\t\tList is full, No user can be added !!! " <<endl;
}
else {
string myData;
UserClass myUser = new UserClass(string& myData);
cout<<"\tFirst Name : " ;
getline(cin,myData);
myUser.setFname(myData);
listUsers[compt].fname = myData;
cout<<"\tLast Name : " ;
getline(cin,myData);
myUser.setLname(myData);
listUsers[compt].lname = myData;
cout<<"\tPhone Number : " ;
getline(cin,myData);
myUser.setPhone(myData);
listUsers[compt].phone = myData;
compt++;
}
return compt;
}
int main(){
.......
}
file2.cpp that I named UserClass.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#ifndef MYUSERS_H
#define MYUSERS_H
#include <iostream>
using namespace std;
class UserClass{
public : //public functions
UserClass(string myData);
UserClass(UserClass& other); //Copy constructor
~UserClass(); // destructor
void setFname(string fname);
string getFname();
void setLname(string lname);
string getLname();
void setPhone(string phoneNumber);
string getPhone();
UserClass& operator =(UserClass& another);//Assignment operator
private : //Private variables
string fname;
string lname;
string phoneNumber;
};
//Setter and getter for first name
void UserClass::setFname(string fname){
this ->fname = fname;
}
string UserClass::getFname(){
return fname;
}
//Setter and getter for last name
void UserClass::setLname(string lname){
this ->lname = lname;
cout<<this ->lname<<" - " <<lname<<endl;
}
string UserClass::getLname(){
return lname;
}
//Setter and getter for phone number
void UserClass::setPhone(string phoneNumber){
this ->phoneNumber = phoneNumber;
}
string UserClass::getPhone(){
return phoneNumber;
}
#endif
I m having trouble getting my program launching for the last 4 days now without finding any sane solution
Last edited on Sep 6, 2016 at 4:50am UTC
Sep 6, 2016 at 5:44am UTC
Try using the call by reference and see if it works
Sep 6, 2016 at 8:18am UTC
When passing parameters you don't specify the type of the var.
Try UserClass myUser = new UserClass(myData);
Sep 6, 2016 at 10:14am UTC
UserClass myUser = new UserClass(myData); is wrong
it should be like
UserClass* myuser=new UserClass(myData)
and rest of the code according to that
myUser.setFname(myData); => myUser->setFname(myData);
Sep 6, 2016 at 5:49pm UTC
What is listUsers[compt] ?
Sep 6, 2016 at 6:19pm UTC
Why does UserClass.cpp have inclusion guards?
Linker errors ... you probably have lingering objects from previous builds that do not correspond to current source.
Your addUser() dynamically allocates/creates object, but does neither delete nor return it. Memory leak error.
Sep 6, 2016 at 6:34pm UTC
UserClass.cpp lines 8-10: You have declared two constructors and a destructor, but I see no implementation for any of these functions. This is going to cause linker errors (undefined symbols).