Advertisement

Help with string class in VC++

Started by August 18, 2002 09:18 PM
10 comments, last by wombatvvv 22 years, 6 months ago
Hi, Obvioulsy I''m just new to C++, having come from a Java background. I went through a whole C++ book in the last few days, and have familiarised myself with everything in the book, it''s really quite similiar to Java. Anyway, I''ve been using Borland''s free command-line compiler to practise, and everything has been working fine. Yesterday, I installed Visual C++ 6.0, and now the string class won''t work properly. I''m sure it''s something small that someone can point out to me. I should point out, that I have no such problems with the Borland compiler, this is only in Visual C++. For example, the following little program gives the following little errors: #include <iostream.h> #include <string.h> main() { string str = "Hello"; return 0; } C:\Documents and Settings\Victor\VICS.CPP(6) : error C2065: ''string'' : undeclared identifier C:\Documents and Settings\Victor\VICS.CPP(6) : error C2146: syntax error : missing '';'' before identifier ''str'' C:\Documents and Settings\Victor\VICS.CPP(6) : error C2065: ''str'' : undeclared identifier C:\Documents and Settings\Victor\VICS.CPP(6) : error C2440: ''='' : cannot convert from ''char [6]'' to ''int'' This conversion requires a reinterpret_cast, a C-style cast or function-style cast The program has got access to the string.h file, because if I misspell it in the include file, it picks that up. An error only occurs if I try and instantiate a string object. Hehe. Obviously I''m not about to be conquering the C++ coding world straight away, but one small step at a time...
OK. It looks like your trying to write a console program instead of a windows program.

Also, a string class is named ''CString,'' and is used in MFC Applications.

I think you''re wanting an MFC Application.

Try playing around on The Code Project until you get a better handle on MS''s flavor of C++.
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Advertisement
CString is part of the STL not MFC isnt it ?
Bad mojo ''round here, yo. Lemme set ya''ll straight here ''n''now...

Okay, the reason it''s giving you the errors is because it thinks you''re trying to include the C-standard string.h library header. What you''ll want to do to fix that (most likely) is change from angle brackets to double quotes (ala "string.h")

Secondly, CString is MFC. string (or, more accurately, std::string) is STL.

Word out,
ZE.

P.S. Ignore the colloquialisms; it''s laaate.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

wombatvvv:

The problem you're having is that you are trying to initialize a string object with C-string syntax. Try this:

#include <iostream>
#include <string>
using namespace std;

//prototype
int main(void);

//main function
int main(void) {

string str("Hello");
return (0);

}

Hope this helps.

John.



[edited by - JohnAD on August 18, 2002 11:48:30 PM]

[edited by - JohnAD on August 18, 2002 11:49:10 PM]
None of that has worked. Thanks for your suggestions, nonetheless.

Surely it can''t be this hard to instantiate a simple, standard string in Visual C++!? It works fine using command-line compilers.

I''m sure it''s a simple problem. The string class is part of the standard C++ libraries. I could move on, but this is so annoying and frustrating. I can''t go on to anything else until I figure out why this doesn''t compile.
Advertisement
JohnAD,

No that didn''t work either. But I didn''t have to do it that way using the Borland command line compiler.

Like2Byte has suggested I''m trying to run a console program in Visual C++...surely Visual C++ can compile anything a free command line compiler can!?

Thanks for your helps guys, keep trying.


JohnAD:

C:\Cpp1.cpp(3) : error C2871: ''std'' : does not exist or is not a namespace
C:\Cpp1.cpp(7) : error C2065: ''string'' : undeclared identifier
C:\Cpp1.cpp(7) : error C2146: syntax error : missing '';'' before identifier ''str''
C:\Cpp1.cpp(7) : error C2065: ''str'' : undeclared identifier
wombatvvv:

I just tried it as a console app. It compiled without error:

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
Linking...

test.exe - 0 error(s), 0 warning(s)

If you're trying to do a Windows app, the problem is probably there. [Edit] If you just want to do console stuff, open a console app. VC++ has certain expectations if you open a windows app.

John.

[edited by - JohnAD on August 18, 2002 12:08:21 AM]
i think this is the code you were tring to do

#include <iostream.h>
#include <string.h>

main()
{
char str[] = "Hello";
return 0;
}
quote:
Original post by Anonymous Poster
CString is part of the STL not MFC isnt it ?

Good - God NO! The popular CString is part of MFC, though I''m sure CString implementations are abound.

The C++ headers that end with a .h are deprecated, they pre-date the standard and are maintained for backwards compatibility. The standard headers have no extention.

The problem is, C++ is a moving target, and you''re aiming at it''s previous location. I''m willing to bet that both the book and the compiler are dated (kinda like programming for Java 1.5 using 1.0 material & tools).
Try picking up "Accelerated C++" (red book, yellow lettering I believe).

#include with quotes tells it to look in the local directory/path - #include with angle brackets <> tells it to look in the library paths set in the IDE environment (Tools->Options).


  #include <string> #include <iostream> #include <sstream>#include <complex>int main(){std::string str;using namespace std;//now we don''t have to type std:: to//access items in the std namespacestr = "Hello World!";cout << str << endl;//srting streams are useful toostd::stringstream ss;int i = 12;double d = 3.14159265358979;std::complex<float> z(.707f, .866f);ss << "This is a string stream.\nIt let''s you pipe stuff into a ";ss << "string buffer\n";ss << typeid(i).name()<<":\t"<<i<<"\n";ss << typeid(d).name()<<":\t"<<d<<"\n";ss << typeid(z).name()<<":\t"<<z<<"\n";//.str() on a string stream gives you a std::string&//that''s a std::string reference//references in C++ can''t be null*cout << ss.str();system("pause");return 0;}  



*If you try really hard you can force one to be null, but they''re not ever suppose to be null.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement