Solved

C++ - String problem.

Posted on 2001-06-11
7
215 Views
Last Modified: 2010-04-02
Hi, I'am kinda new at C++
and I don't know how to solve this problem.

I tryed this little program and it does'nt work.

#include <iostream.h>
#include <string.h>     // include for C++ standard string class

void main()
{
    string TestString = "a string";
    cout << "Length of my string : " << TestString.length;
}
_________________________________________________

Here's the error that I get when I compile.

--------------------Configuration: mytest - Win32 Debug--------------------
Compiling...
mytest.cpp
C:\mytest.cpp(6) : error C2065: 'string' : undeclared identifier
C:\mytest.cpp(6) : error C2146: syntax error : missing ';' before identifier 'TestString'
C:\mytest.cpp(6) : error C2065: 'TestString' : undeclared identifier
C:\mytest.cpp(6) : error C2440: '=' : cannot convert from 'char [9]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\mytest.cpp(7) : error C2228: left of '.length' must have class/struct/union type
Error executing cl.exe.

mytest.obj - 5 error(s), 0 warning(s)
______________________________________________________

Could someone help me please.
0
Comment
Question by:deschenesp
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6177634
You'll have to use the 'std' namespace to make it work this way:

#include <iostream>
#include <string>     // include for C++ standard string class

using namespace std;

void main()
{
   string TestString = "a string";
   cout << "Length of my string : " << TestString.length;
}

BTW: Note that I removed the trailing '.h' from your include directives, the most actual files come without that extension...
0
 
LVL 2

Expert Comment

by:joe_h
ID: 6177669
Try just
#include<string>

instead of
#include<string.h>
0
 
LVL 2

Expert Comment

by:joe_h
ID: 6177674
okay jkr, you were faster :-))))
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:deschenesp
ID: 6177714
Thank's for the tip jkr,

  But I still get error when I compile and
the TestString.length give 1.

Here the error when I compile

--------------------Configuration: mytest - Win32 Debug--------------------
Compiling...
mytest.cpp
C:\mytest.cpp(9) : warning C4761: integral size mismatch in argument; conversion supplied
Linking...

mytest.exe - 0 error(s), 1 warning(s)


0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6177762
Try

cout << "Length of my string : " << TestString.length();

cout << "Length of my string : " << TestString.size();

instead. Without the '()' it is interpreted as the address of a method, thus the conversion warning...
0
 
LVL 86

Expert Comment

by:jkr
ID: 6177833
BTW - I just found almost the same sample in the VC++ docs:

#include <iostream>
#include <string>

using namespace std;


void main()
{
    string TestString = "1111122222333334444455555";
    cout << TestString << "\n  size: " << TestString.size() << endl;
    TestString.resize(5);
    cout << TestString << "\n  size: " << TestString.size() << endl;
    TestString.resize(10);
    cout << TestString << "\n  size: " << TestString.size() << endl;
    TestString.resize(15,'6');
    cout << TestString << "\n  size: " << TestString.size() << endl;
}

0
 

Author Comment

by:deschenesp
ID: 6177860
Thank's a lot jkr,


     I finally found the last part by my own but thank's for the rest.

         You're a genius.
         Good job.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question