today when I tried some of member functions which i read about them in a cpp course i found in my c++ compiler (which is visual c++ 6) this error:
error C2228: left of '.length' must have class/struct/union type
when i used this member function:

str_name.length()

instead of strlen() function>
so :is the problem from the old compiler or what!
and what is the different between strlen() & str_name.length()

thank you all :icon_wink:

You want to include <string> instead of <string.h> to declare it as a std::string which has a length() method (really just an alias for the size() method). strlen() comes in <string.h> (or <cstring> in a compiler that conforms closely to the standard (which VC++ 6 does not)) and is used on null terminated C style strings. How is str_name defined?

That's rather terse but post back for clarification or google for std::string versus C style strings and there should be a ton of hits.

It looks like you did not declare a string.

#include <string>
using namespace std;
string str;
str.length()

my code is

#include <string.h>
#include <iostream.h>
#include "genlib.h"
int main()
{
string str="Yes, we went to Gates after we left the dorm.";  ";
int a=str.find("we",0)//strat with the begining of the line
int b=str.find("we",a+1)//strat with the a+1 character and so on....
cout<<"first position is :"<<a<<endl;
cout<<"second position is :"<<b<<endl;
int pos;
str.find('m')
if(pos==string::npos)//if he didn't find any match 
cout<<"no m hear!"<<endl;
else
cout<<"there is m hear !"<<endl;
}

please attention that is used genlib.h :instead of std::string

You want to include <string> instead of <string.h> to declare it as a std::string

the course at:link
said :

you’ll need to include genlib.h to make the short name
string visible instead of requiring the cumbersome std::string.

Yes, but even in that same handout they have you include

#include <iostream>
#include <string>  // != string.h (I'm not sure how this is resolved in pre standard compilers)
#include "genlib.h" //which must be a custom header to go with their course

If you're just starting out and you're not attached to VC++ 6 due to legacy code or needing MFC, grab the VC++ 2010 Express Edition compiler from M$. It's free and is infinitely more standard compliant than v.6.

you’ll need to include genlib.h to make the short name
string visible instead of requiring the cumbersome std::string.

All this means is that the header probably has using namespace std; (or using std::string by itself) so that you don't have to qualify the name (typing std::string instead of typing string, just as you would have to for std::cout, etc) it doesn't have any bearing on anything else.

anyway i think my problem solved.... thanks jonsca .so i can say if i used length() with standard library i would not have any problem Right ?

thank you everything goes Right:

#include <string>
#include <iostream>
	using namespace std;
void main()
{


string str="we all love peace yes! we do  ";
int a=str.find("we",0);//strat with the begining of the line
int b=str.find("we",a+1);//strat with the a+1 character and so one>>>>>>>>>
cout<<"first position is :"<<a<<endl;
cout<<"second position is :"<<b<<endl;
int pos;
pos=str.find('m');
if(pos==string::npos)
cout<<"no m hear!"<<endl;
else
cout<<"there is m hear !"<<endl;
}

output:
first position is :0
second position is :34
there is m hear !
Press any key to continue

Classes can have member variables and methods.

class Example //this could just as easily be a struct as both it's members are public
{
    public:
        int exampleint;
        void examplemethod() { //do something }
};

int main()
{
  int num;
  Example ex;
  num.method();  //int is a native type not a class struct or union, can't use the .(it has no methods)
  //the above line would generate a message similar to the one you had, I believe (I didn't compile it and can't remember)
  num = ex.exampleint; //ok we can access exampleint by the .
  ex.examplemethod(); //works as ex is an object of a class
  return 0;
}

However, in your case it is because it is not recognizing the type std::string because your headers are too old (at least that what it seems like).

Glad it worked!

void main()

You had it right the first time with int main. main returns an int according to the standard.

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.