Solved

C++ - String problem.

Posted on 2001-06-11
7
212 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
std::atomic with custom class 6 190
SetCurrentDirectory path limit 7 106
Should CArray be used for a list of pointers in C++? 19 97
is twain_32.dll cmpatible with windows 10 ? 10 129
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now