Solved

C++ - String problem.

Posted on 2001-06-11
7
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

636 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