Solved

C++ - String problem.

Posted on 2001-06-11
7
211 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
Comment Utility
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
Comment Utility
Try just
#include<string>

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

Expert Comment

by:joe_h
Comment Utility
okay jkr, you were faster :-))))
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:deschenesp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

16 Experts available now in Live!

Get 1:1 Help Now