beginner question.

Posted by kenny31539 on Wed 31 Mar 2010 10:38 PM — 7 posts, 27,812 views.

USA #0
Ok, this might seem like a rather silly question. I'm not-so-new at coding, but I still don't know my tail-end from a hole in the ground. I've managed to track down and fix a few bugs since updating to ubuntu's Koala, but this one is simply giving me fits.

void Project_Handler::set_topwidth(void)
{
	Project_Data *project;
	string strbuf;
	if(!first_project)
	{
		topwidth = 0;
		return;
	}
	
	for(project = first_project; project; project = project->get_next())
	{
		strbuf = project->get_heading();
		if(topwidth < (int)strlen(strbuf.c_str()))
		{
			topwidth = strlen(strbuf.c_str());
		}
	}
	return;
}

Now, the error that it's throwing when I compile is strlen is undeclared in this scope. So, I tried the logical thing and tried declaring it...and it threw another error saying th at strlen can't be used as a descriptor or some such (I lost my password so waiting for the admin to show up)

Anyone familliar at all with this? OH! Idiota me...it's in swfotefuss.

Any help or advice (other than suicide or burning my fingertips off with a blowtorch) would be greatly appreciated. :)
#1
You might want to check if strings.h is declared at the top of the file. It should look like

#include <string.h>

USA #2
Ever...just really feel like an idiot? I took a look at the top of the file and...what do you know...the only header there was project.h Ok ok...so maybe I DO need to take a blowtorch to my fingers. Thanks a bundle! Soon as the admin comes around I'll let you know if it worked or not. :P
USA #3
Your way is a little roundabout. You could always change:


strlen(strbuf.c_str())


to:


strbuf.length()


:)
USA #4
yeah, but:



	if(topwidth < (int)strlen(strbuf.c_str()))




is the line that's throwing the error. Tried changing it to


if(topwidth <strlen(strbuf.c_str) );

and putting


int strbuf]

earlier in the function, but then it gave me some other screwy error I can't remember at the moment.
USA #5
strbuf is already declared as a string, you wouldn't want to define it as an integer.


if(topwidth <strlen(strbuf.c_str) );


There are a couple things wrong with that statement. First, if strlen is undeclared you will still get the same error you were before.

If you redefined strbuf to be an int then it would throw an error about calling c_str.

The correct way to call the c_str function is
strbuf.c_str()

So you are missing parens and it would give you a syntax error.

The semi-colon would not trigger an error, but would almost certainly not do what you would expect. It signifies an empty block, thus nullifying the code the if statement was supposed to execute. Conditional statements should not have semi-colons after them unless it is intended, which is typically rare.

I see no reason why this wouldn't work:

void Project_Handler::set_topwidth(void)
{
	Project_Data *project;
	string strbuf;

	if(!first_project)
	{
		topwidth = 0;
		return;
	}
	
	for(project = first_project; project; project = project->get_next())
	{
		strbuf = project->get_heading();

		if(topwidth < strbuf.length())
		{
			topwidth = strbuf.length();
		}
	}
}

Amended on Thu 01 Apr 2010 03:55 AM by Nick Cash
USA #6
Turned out that adding strings.h to the header worked like a charm. :) Much appreciate for all the help! I'm sure that I'll come up with a million more questions and seeking advice in the very near future. :)