I am trying to copy a string into an array then write bits out to a file accordingly. (compression assignment). here is the code

// strnewdup(const char* s) returns a copy of a
// null-terminated string, with the copy stored
// in the heap
char* strnewdup(const char* s)
{
	char* space = new char[strlen(s) + 1];
	strcpy(space, s);
	return space;
}

// putCodes(Node*& t, string* codes) traverses
// the tree t, recording the non-leaves(0) and
// leaves(1) into the correct letters code array
// to identify that letter
void putCodes(Node* t, string s, string* codes)
{
	if(t->kind == leaf)
	{
		const char* c = s.c_str();
		codes[(int)t->ch] = c;
	}
	else
	{
		putCodes(t->left, s+"0", codes);
		putCodes(t->right, s+"1", codes);
	}
}

ONE I AM HAVING TROUBLE WITH
// encFile(cont char* argv[], BFILE*& f, string* code)
// encodes the file argv and using the codes from code
// array puts them into file f
void encFile(const char* argv, BFILE*& f, string* code)
{
	string* s;
	s = code;
	const char* c;
	c = s.c_str();
	int length = strlen(s);
}

Here is the error I am getting

huffman.cpp: In function ‘void encFile(const char*, BFILE*&, std::string*)’:
huffman.cpp:164: error: request for member ‘c_str’ in ‘s’, which is of non-class type ‘std::string*’
huffman.cpp:165: error: cannot convert ‘std::string*’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
huffman.cpp:165: warning: unused variable ‘length’
make: *** [huffman] Error 1

They are both string* are they not? Also, I have tried const char* and that does not work as well. Any thoughts? the code that is passed is something such as "001" or "0011" and I want to somehow read this char by char and then use a writeBit(f,0) function to write that bit to a file f.

Recommended Answers

All 3 Replies

Dereference s before you try to get the c_str() method: c = (*s).c_str(); since s is a pointer to string not a string. Or c=s->c_str(); will work too I just thought the other form was more illustrative for your benefit.

Also, strlen won't give you the length of the std::string, use s->length() (same need to dereference) instead-- unless you meant to take strlen of c instead.

You do know you can say s[0], s[1], s[n] to get the characters of a string, don't you?

Dereference s before you try to get the c_str() method: c = (*s).c_str(); since s is a pointer to string not a string. Or c=s->c_str(); will work too I just thought the other form was more illustrative for your benefit.

Also, strlen won't give you the length of the std::string, use s->length() (same need to dereference) instead-- unless you meant to take strlen of c instead.

got it :) thank you. though i didnt use this method, instead i just used the ofstream feature of append. appreciate the feedback though :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.