I get this error: from 'std::basic_string<char>' to 'bool'

Here is the complete error:
C:\Dev-Cpp\MAlikChapter1\personTypeDriver.cpp:13:60: error: could not convert 'personType::getLast(std::string) const(std::basic_string<char>(((const char*)"Motloung"), (*(const std::allocator<char>*)(& std::allocator<char>())))).std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((*(const std::basic_string<char>*)(& personType::getLast(std::string) const(std::basic_string<char>(((const char*)"Motloung"), (*(const std::allocator<char>*)(& std::allocator<char>())))))))' from 'std::basic_string<char>' to 'bool'
if(myName.getLast("Motloung") = myName.getLast("Motloung"))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  Put the code you need help with here.

#include<iostream>
#include<cstring>
#include "personTypeImp.cpp"

using namespace std;

int main()
{
	personType myName("Martin", "Bopaki", "Motloung");
	cout<<"Enter the last name: " << endl;

	cin>> lastName;
	if(myName.getLast("Motloung") = myName.getLast("Motloung"))
		cout<<"They are the same" <<endl;
	//else
		cout<<"They are not the same." <<endl;	
}

Here is the Implementation file:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstring>

#include<iostream>

using namespace std;

class personType
{
public:
    void print() const;
		//Function to output the first name and last name
		//in the form firstName lastName
  
    void setName(string first, string middle, string last);
		//Function to set firstName and lastName according to 
 		//the parameters
			//Postcondition: firstName = first; lastName = last  

    string getName(string first, string middle, string last);
		//Function to return firstName and lastName via the 
		//parameters
		//Postcondition: first = firstName; last = lastName
	string getFirst(string first) const;
	string getLast(string last) const;	

    personType(string first = "", string middle = "", string last = "");
		//Constructor
		//Set firstName and lastName according to the parameters
 		//The default values of the parameters are empty strings
 		//Postcondition: firstName = first; lastName = last  

 private:
    string firstName; //stores the first name
    string lastName;  //stores the last name
    string middleName;
};
//#include <string>
//using namespace std;

//class personType
//{
//public:
  //  void print() const;
		//Function to output the first name and last name
		//in the form firstName lastName
  
    void setName(string& first, string& middle, string& last);
		//Function to set firstName and lastName according to 
 		//the parameters
			//Postcondition: firstName = first; lastName = last  
	void setLast(string last);
	void setFirst(string first);
	void setMiddleName(string middle);
    string getName(string& first, string& middle, string& last);
		//Function to return firstName and lastName via the 
		//parameters
		//Postcondition: first = firstName; last = lastName

  //  personType(string first = "", string middle = "", string last = "");
		//Constructor
		//Set firstName and lastName according to the parameters
 		//The default values of the parameters are empty strings
 		//Postcondition: firstName = first; lastName = last  

 //private:
    string firstName; //stores the first name
    string lastName;  //stores the last name
    string middleName;
//};

//#include <iostream>
//#include "person.h"

//using namespace std;


void personType::print() const
{
	cout<<firstName<<" "<<lastName;
}

void personType::setName(string first, string middle, string last)
{
	firstName = first;
	middleName = middle;
//void personType::setMiddle(string middle) const
//{
//	middleName = middle;
			}			
string personType::getName(string first, string middle, string last)
{
	//first = firstName;
	//middle = middleName;
	//last = lastName;
	return firstName , middleName, lastName;
}
string personType::getFirst(string first) const
{
	//first = firstName;
	return firstName;
}
	string personType::getLast(string last) const
	{
	//	last = lastName;
	return lastName;
	}
//constructor 
personType::personType(string first, string middle, string last) 

{ 
	firstName = first;
	middleName = middle;
	lastName = last;
}


Here is the header file:
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
#include<iostream>
#include<cstring>
#ifndef PERSONTYPE_H
#define PERSONTYPE_H

using namespace std;

class personType
{
public:
    void print() const;
		//Function to output the first name and last name
		//in the form firstName lastName
  
    void setName(string first, string last);
		//Function to set firstName and lastName according to 
 		//the parameters
		//Postcondition: firstName = first; lastName = last

    void getName(string& first, string& last);
		//Function to return firstName and lastName via the 
		//parameters
		//Postcondition: first = firstName; last = lastName

    personType(string first = "", string last = "");
		//Constructor
		//Set firstName and lastName according to the parameters
 		//The default values of the parameters are empty strings
 		//Postcondition: firstName = first; lastName = last  

 private:
    string firstName; //stores the first name
    string lastName;  //stores the last name
};
#endif //PERSONTYPE_H
I think you have a design issue.

You are doing something familiar to a thread. Check [PersonalInfo.h and PersonalInfo.cpp] here
http://www.cplusplus.com/forum/beginner/219213/#msg1010522

Good luck
The problem is this:

if(myName.getLast("Motloung") = myName.getLast("Motloung"))

actually the assignment operator = . I would think that you want the comparison operator == .

The getters in personType don't make too much sense. You always provide parameter you don't use/makes no sense, why?

It is unlikely that this:
1
2
3
4
5
6
7
string personType::getName(string first, string middle, string last)
{
	//first = firstName;
	//middle = middleName;
	//last = lastName;
	return firstName , middleName, lastName;
}
does what you think it does. Actually it returns lastName. You probably want this:
1
2
3
4
5
6
7
string personType::getName(string first, string middle, string last)
{
	//first = firstName;
	//middle = middleName;
	//last = lastName;
	return firstName + middleName + lastName; // Note + instead of ,
}
Hello Bopaki,

I agree with blongho you have a design issue.

In the header file line 2 should be "string" not "cstring". More important is that lines 1 and 2 should not be there i the first place.

The function void getName(string& first, string& last); is all wrong. Being a get function it needs to return something and there should be two get functions:

1
2
    std::string getFirstName();
    std::string getLastName();


Both functions need only return there is no need for any parameters.

Under "private:" I think you are missing a variable for middle name?

In the implementation file again "cstring" should be "string". You have redefined the class when all you need is #include ClassFileName.h at the beginning of the file. Also notice that the redefinition of the class is different than what is in the header file.

Both in the header file and the implementation file you have the line using namespace std;. This should never be in a header file. See:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Starting at line 71 that is what should be in the implementation file. It should start like this:

1
2
3
4
5
6
#include <iostream>
#include <string>

#include "person.h"

// followed by the functions of the class. 


I will have to rework the files to get rid of some of the errors and clean up the file before I can see what other problems come up.

Hope that helps,

Andy
Last edited on
Hello Bopaki,

I manged o get your program to work. I kept most of what you started with. Added some things, notice the comments in the sections, and rearranged some parts of the files.

Main file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

#include "Person Type.h"

int main()
{
	std::string lastName{ "" };  // <--- Added for line 12.
	personType myName("Martin", "Bopaki", "Motloung");

	std::cout << "Enter the last name: " << std::endl;
	std::cin >> lastName;  // <--- Now never used for anything.

	// comparing the same last name.
	if (myName.getLast() == myName.getLast())  // <--- I think you wnated "==" here. Think about what you are doing. the if statement will always be true.
		std::cout << "They are the same" << std::endl;
	else  // <--- Uncommented this.
		std::cout << "They are not the same." << std::endl;

}


Header file:
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
#ifndef PERSONTYPE_H
#define PERSONTYPE_H

class personType
{
public:
	void print() const;
	//Function to output the first name and last name
	//in the form firstName lastName


	void setName(std::string first, std::string middle, std::string last);
	//Function to set firstName and lastName according to
	//the parameters
	//Postcondition: firstName = first; lastName = last

	//  <--- Added these functions.
	void setLastName(std::string last);
	void setFirstName(std::string first);
	void setMiddleName(std::string middle);

	std::string getName();
	//Function to return firstName and lastName via the
	//parameters
	//Postcondition: first = firstName; last = lastName

	//  <--- Added these functions.
	std::string getFirst() const;  // <--- Removed the parameters.
	std::string getMiddle() const;// <--- Removed the parameters.
	std::string getLast() const;// <--- Removed the parameters.

	personType(std::string first = "", std::string middle = "", std::string last = "");
	//Constructor
	//Set firstName and lastName according to the parameters
	//The default values of the parameters are empty strings
	//Postcondition: firstName = first; lastName = last

private:
	std::string firstName; //stores the first name
	std::string lastName;  //stores the last name
	std::string middleName;
};
#endif //PERSONTYPE_H 


Implementation file:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include <string>

#include "Person Type.h"  // <--- I tend to make the file name the same as the class name.

void personType::print() const
{
	std::cout << firstName << " " << lastName;  //<--- Needs middle name.
}

//  These next four functions are OK, but never used.
void personType::setName(std::string first, std::string middle, std::string last)
{
	firstName = first;
	middleName = middle;
	lastName = last;
}

//  Added these three functions.
void personType::setFirstName(std::string first)
{
	firstName = first;
}

void personType::setMiddleName(std::string middle)
{
	middleName = middle;
}

void personType::setLastName(std::string last)
{
	lastName = last;
}

std::string personType::getName()
{
	//first = firstName;
	//middle = middleName;
	//last = lastName;
	return firstName + " " + middleName + " " + lastName;  // <--- Added the space for readability.
}

// Next three functions OK, but never used.
std::string personType::getFirst() const
{
	//first = firstName;
	return firstName;
}

std::string personType::getMiddle() const { return middleName; }  // <--- Added.

std::string personType::getLast() const
{
	//	last = lastName;
	return lastName;
}

//constructor 
personType::personType(std::string first, std::string middle, std::string last)

{
	firstName = first;
	middleName = middle;
	lastName = last;
}


My file name for the header file and the .cpp file are different because I did not find the file name that you used until after I had started.

What is your intention with main? With what you started with there is no real point to the code. Line 12 asks for a "lastName, but this variable was never defined. this i a compile error. I think what yo want to do with the if statement is: if (myName.getLast() == lastName), but even this has no real use except for a spelling check because there is only one instance of the class to work with. As part of a for loop to check an array or better a vector of classes each with element having different names it would make more sense. With a comment on the else the next line is not part of the if statement and will always print. Not what you want.

I think this should give you some ideas of where you went wrong.

Hope this helps,

Andy
Thanks to everyone.

The program now runs fine and gives the correct output.
Needs a lot of improvements though.

I sometimes get confused and end up running around like a chicken without
its head.
Topic archived. No new replies allowed.