.c_str() Error

I'm getting an error when using .c_str(). The error is "left of '.find' must have class/struct/union". Can someone help me?

I've included the .cpp code but not the .h code.



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
/* Write a C++ program to do the following :

Inputs a line of text.
Tokenizes the line into separate words.
Inserts the words into a binary search tree(BST).
Do a postorder traversal of the tree and print it.
Do a preorder traversal of the tree and print it.
Do an inorder traversal of the tree and print it.
Print the heights and the number of leafs in each of the three binary search trees.*/

#include "Tree.h"


int main() 
{

	BTreeType<string> wordTree;
	char *line;
	char *words;
	ifstream inData;
	int n = 0;


	inData.open("Text.txt");

	getline(inData, line.c_str());


	words = strtok(line, " .");
	while (words != NULL)
	{
		wordTree.insert(words);
		words = strtok(line, " ,.");
		n++;
	}

	inData.close();

	cout << "Preorder Transversal:" << endl;
	wordTree.preOrderTraversal();
	cout << endl;

	cout << "Postorder Transversal:" << endl;
	wordTree.postOrderTraversal();
	cout << endl;

	cout << "Inorder Transversal:" << endl;
	wordTree.inOrderTraversal();
	cout << endl;


	system("pause");
	return 0;
}
Last edited on
PLEASE learn to use code tags, it makes reading and commenting on your source MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add the code tags.

You really do need to post the header file as well.
Last edited on
.c_str() is a member function for std::string. You are trying to call it on line which is a char*.

std::string.c_str() returns a const char*.

There are 2 versions of getline that apply here. The first is the non-member function istream& getline (istream& is, string& str); (http://www.cplusplus.com/reference/string/string/getline/). In this case, you need to pass a reference to a std::string.

The second version is the member function istream& istream::getline (char* s, streamsize n ); (http://www.cplusplus.com/reference/istream/istream/getline/). In this case you need to pass both a char* and a length. The char* must point to memory that can be written to.

You seem to be conflating these 2 methods.

I would suggest you replace char *line; with std::string line; and replace getline(inData, line.c_str()); with getline(inData, line);.
Last edited on
Thanks for the input. Unfortunately, that is how my instructor told me to do it, thus why I did it that way.

When I change those line to string line, I get the error message 'char *strtok(char *,const char *)': cannot convert argument 1 from 'std::string' to 'char *'. Any suggestions?
don't mix and match the C and C++ as a beginner. Stick to char* or string, but not a mix.

if your school is using C and char*, just use that, and strtok will be ok. Use the getline for char*, as said above, looks like

char charptr[101];
cin.getline(charptr, 100); //whatever max length you want


arrays of char are cleaner. if you use a pure pointer, you need to manage the memory, which you didn't do above.
char *annoying;
annoying = new char[100]; //get memory from OS
… use it
delete[] annoying; //give it back to OS

the name of an array of char is a char* to the compiler:
char x[10];
x, without any [] notation, just x, is treated like a char* for the most part when passed to the C string functions.
Last edited on
I suspect the instructor wanted the return value of std::string::c_str() to be passed as the first parameter to strtok(), which is pretty idiotic, because strtok() modifies that string, so it would result in undefined behavior.
Thanks for the suggestions. My instructor ended up helping me; I'm pretty sure he gets these problems online and never actually does them.
Topic archived. No new replies allowed.