Problem to execute

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
Try using the call by reference and see if it works
When passing parameters you don't specify the type of the var.
Try UserClass myUser = new UserClass(myData);
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);
nothing works .

When I tried "sylphsang" 's solution , i get this message error:
1
2
3
4
5
Undefined symbols for architecture x86_64:
  "Contact::Contact(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      addUser(int) in useContact-6991ef.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


the call by reference does not work either.
either the solution proposed by Thomas. that one shows me an error message saying :
Not viable conversion .
What is listUsers[compt] ?
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.
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).

Topic archived. No new replies allowed.