Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ - String problem.

Posted on 2001-06-11
7
Medium Priority
?
221 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 200 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

715 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