By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,743 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,743 IT Pros & Developers. It's quick & easy.

Problem using string/vector in MS Visual C++ 6/g++

P: n/a
I run the following code in UNIX compiled by g++ 3.3.2 successfully.

: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using namespace std;
:
: vector<string>* getTyphoon()
: {
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
: string A = typhoonList.at(0);
: cout << "A: " << A << endl;
:
: return 0;
: }
But as I transfer the code to MS Visual C++, error appears.
What make me frustrated is that after certain modification, I compiled
"successfully" (with some warnings), but the program still hangs during
runtime (the WinXP error reporting dialogue is appeared).

(1) Anyone kindly knows what's the problem? (Following is the code after
modification)

: // proj2.cpp : Defines the entry point for the console application.
: //
: #include "stdafx.h"
: #include <iostream>
: #include <vector>
:
: using namespace std;
:
: vector<string>* getTyphoon(){
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
:
: string A = typhoonList.at(0);
: printf("%s\n", A);
:
: return 0;
: }
(2) I tried to compile the second code in UNIX again... (that's using the
"printf" and removal of #include "stdafx.h"), the following error is out,
what does it mean?

: user@honest user> g++ test.cc -o test
: test.cc: In function `int main(int, char**)':
: test.cc:26: warning: cannot pass objects of non-POD type `struct
std::string'
: through `...'; call will abort at runtime
: user@honest user>
It would be glad if anyone can help me solve the problem...
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Forecast wrote:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.

: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using namespace std;
:
: vector<string>* getTyphoon()
: {
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
: string A = typhoonList.at(0);
: cout << "A: " << A << endl;
:
: return 0;
: }
But as I transfer the code to MS Visual C++, error appears.
What make me frustrated is that after certain modification, I compiled
"successfully" (with some warnings), but the program still hangs during
runtime (the WinXP error reporting dialogue is appeared).

(1) Anyone kindly knows what's the problem? (Following is the code after
modification)

: // proj2.cpp : Defines the entry point for the console application.
: //
: #include "stdafx.h"
: #include <iostream>
: #include <vector>
:
: using namespace std;
:
: vector<string>* getTyphoon(){
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
:
: string A = typhoonList.at(0);
: printf("%s\n", A);
:
: return 0;
: }
(2) I tried to compile the second code in UNIX again... (that's using the
"printf" and removal of #include "stdafx.h"), the following error is out,
what does it mean?

: user@honest user> g++ test.cc -o test
: test.cc: In function `int main(int, char**)':
: test.cc:26: warning: cannot pass objects of non-POD type `struct
std::string'
: through `...'; call will abort at runtime
: user@honest user>
It would be glad if anyone can help me solve the problem...


It sounds like MS has an entity in the global or std namespace called A.
Change the variable name, perhaps to "a".

Btw, please replace "return 0" with "delete typhoonList".
Jul 22 '05 #2

P: n/a
On Sat, 3 Jan 2004 11:51:49 +0800, "Forecast" <fo******@csis.hku.hk>
wrote in comp.lang.c++:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.

: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using namespace std;
:
: vector<string>* getTyphoon()
: {
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
: string A = typhoonList.at(0);
: cout << "A: " << A << endl;
:
: return 0;
: }
But as I transfer the code to MS Visual C++, error appears.
What version of Visual C++? What errors? Up until quite recently,
Visual C++ remained way behind in ISO C++ standard conformance. GCC
3.3.x, on the other hand, probably has quite a bit better conformance
than the version of Visual C++ you are using.
What make me frustrated is that after certain modification, I compiled
"successfully" (with some warnings), but the program still hangs during
runtime (the WinXP error reporting dialogue is appeared).

(1) Anyone kindly knows what's the problem? (Following is the code after
modification)

: // proj2.cpp : Defines the entry point for the console application.
: //
: #include "stdafx.h"
: #include <iostream>
: #include <vector>
You need to add either:

#include <stdio.h>

....or:

#include <cstdio>

....here.

The C++ standard, unlike the C standard, allows ANY standard header to
include ANY OTHER standard header, but does not guarantee that it will
do so. If you want to use printf() in a C++ program, you should
include this header in one of its two forms yourself.
:
: using namespace std;
:
: vector<string>* getTyphoon(){
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
:
: string A = typhoonList.at(0);
: printf("%s\n", A);
The %s conversion to printf() requires a C string argument, that is an
array of characters including a terminating '\0' character. Not a C++
std::string. printf() is a function inherited from C, and it does not
deal with C++ std::strings at all.

If you want to pass a C++ std::string to any C library function that
expects a C string, you need to use the c_str member function:

printf("%s\n", A.c_str());
:
: return 0;
: }
(2) I tried to compile the second code in UNIX again... (that's using the
"printf" and removal of #include "stdafx.h"), the following error is out,
what does it mean?

: user@honest user> g++ test.cc -o test
: test.cc: In function `int main(int, char**)':
: test.cc:26: warning: cannot pass objects of non-POD type `struct
std::string'
: through `...'; call will abort at runtime
: user@honest user>
It would be glad if anyone can help me solve the problem...


If you must build code to work with two different compilers, you need
to make sure that they do not have vastly different levels of C++
standard conformance, or you will continue to have these kind of
problems.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3

P: n/a
thx.
Jul 22 '05 #4

P: n/a
I am using MS Visual C++ 6.0 (Prof. ed.). I can compile the program in VC++
now!
(I use g++ previously juz for comparision, but no need cross platform dev..)

But I have one more question, after modifying the code:

: // proj2.cpp : Defines the entry point for the console application.
: //
:
: #include "stdafx.h"
: #include <iostream>
: #include <vector>
: #include <string>
: #include <cstdio>
:
: using namespace std;
:
: vector<string>* getTyphoon(){
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
:
: string A = typhoonList.at(0);
: cout << "A: " << A << endl;
:
: return 0; // suppose i don't want to delete the dynamic location yet
: }

VC++ still issue 5 warnings (I usually don't know what it means), any
problem with that?

: --------------------Configuration: proj5 - Win32 Debug--------------------
: Compiling...
: proj5.cpp
: D:\proj5\proj5.cpp(19) : warning C4786:
'std::reverse_iterator<std::basic_string<char,std: :char_traits<char>,std::al
locator<char> > const
*,std::basic_string<char,std::char_traits<char>,st d::allocator<char>
,std::basic_string<char,std::char_traits< : char>,std::allocator<char> > const
&,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > const
*,int>' : identifier was truncated to '255' characters in the debug
information
: D:\proj5\proj5.cpp(19) : warning C4786:
'std::reverse_iterator<std::basic_string<char,std: :char_traits<char>,std::al
locator<char> >
*,std::basic_string<char,std::char_traits<char>,st d::allocator<char>,std::basic_string<char,std::char_traits<char>, : std::allocator<char> >
&,std::basic_string<char,std::char_traits<char>,st d::allocator<char> >
*,int>' : identifier was truncated to '255' characters in the debug
information
: c:\program files\microsoft visual studio\vc98\include\vector(39) : warning
C4786:
'std::vector<std::basic_string<char,std::char_trai ts<char>,std::allocator<ch
ar>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato r<char> > >
:::vector<std::basic_string<char,std::char_traits< char>,std::allocator<char>
,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato r<char> > > >' : identifier was truncated to '255' characters in the debug
information
: c:\program files\microsoft visual studio\vc98\include\vector(52) : warning
C4786:
'std::vector<std::basic_string<char,std::char_trai ts<char>,std::allocator<ch
ar>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato r<char> > >
:::vector<std::basic_string<char,std::char_traits< char>,std::allocator<char>
,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato r<char> > > >' : identifier was truncated to '255' characters in the debug
information
: c:\program files\microsoft visual studio\vc98\include\vector(60) : warning
C4786:
'std::vector<std::basic_string<char,std::char_trai ts<char>,std::allocator<ch
ar>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato r<char> > >
:::~vector<std::basic_string<char,std::char_traits <char>,std::allocator<char

,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato r<char> > > >' : identifier was truncated to '255' characters in the debug
information
: Linking...
:
: proj5.exe - 0 error(s), 5 warning(s)

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:4a********************************@4ax.com... On Sat, 3 Jan 2004 11:51:49 +0800, "Forecast" <fo******@csis.hku.hk>
wrote in comp.lang.c++:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.

: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using namespace std;
:
: vector<string>* getTyphoon()
: {
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
: string A = typhoonList.at(0);
: cout << "A: " << A << endl;
:
: return 0;
: }
But as I transfer the code to MS Visual C++, error appears.


What version of Visual C++? What errors? Up until quite recently,
Visual C++ remained way behind in ISO C++ standard conformance. GCC
3.3.x, on the other hand, probably has quite a bit better conformance
than the version of Visual C++ you are using.
What make me frustrated is that after certain modification, I compiled
"successfully" (with some warnings), but the program still hangs during
runtime (the WinXP error reporting dialogue is appeared).

(1) Anyone kindly knows what's the problem? (Following is the code after
modification)

: // proj2.cpp : Defines the entry point for the console application.
: //
: #include "stdafx.h"
: #include <iostream>
: #include <vector>


You need to add either:

#include <stdio.h>

...or:

#include <cstdio>

...here.

The C++ standard, unlike the C standard, allows ANY standard header to
include ANY OTHER standard header, but does not guarantee that it will
do so. If you want to use printf() in a C++ program, you should
include this header in one of its two forms yourself.
:
: using namespace std;
:
: vector<string>* getTyphoon(){
: vector<string>* typhoonList = new vector<string>();
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
:
: string A = typhoonList.at(0);
: printf("%s\n", A);


The %s conversion to printf() requires a C string argument, that is an
array of characters including a terminating '\0' character. Not a C++
std::string. printf() is a function inherited from C, and it does not
deal with C++ std::strings at all.

If you want to pass a C++ std::string to any C library function that
expects a C string, you need to use the c_str member function:

printf("%s\n", A.c_str());
:
: return 0;
: }
(2) I tried to compile the second code in UNIX again... (that's using the "printf" and removal of #include "stdafx.h"), the following error is out, what does it mean?

: user@honest user> g++ test.cc -o test
: test.cc: In function `int main(int, char**)':
: test.cc:26: warning: cannot pass objects of non-POD type `struct
std::string'
: through `...'; call will abort at runtime
: user@honest user>
It would be glad if anyone can help me solve the problem...


If you must build code to work with two different compilers, you need
to make sure that they do not have vastly different levels of C++
standard conformance, or you will continue to have these kind of
problems.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Jul 22 '05 #5

P: n/a
Its a MS thing... I think templated type names can only be 255 chars long
for some compatibility reason. Here is how to get rid of the warnings:
// Put this at the top of your source file/ header file
#ifdef _MSC_VER
#pragma warning (disable: 4786)
#endif //_MSC_VER

HTH,
S. Armondi

Jul 22 '05 #6

P: n/a
thx.

M$ no good.

"Samuele Armondi" <sa****************@hotmail.com> wrote in message
news:3f********@mk-nntp-1.news.uk.worldonline.com...
Its a MS thing... I think templated type names can only be 255 chars long
for some compatibility reason. Here is how to get rid of the warnings:
// Put this at the top of your source file/ header file
#ifdef _MSC_VER
#pragma warning (disable: 4786)
#endif //_MSC_VER

HTH,
S. Armondi

Jul 22 '05 #7

P: n/a
Forecast wrote:
I am using MS Visual C++ 6.0 (Prof. ed.). I can compile the program in
VC++ now!
(I use g++ previously juz for comparision, but no need cross platform
dev..)

But I have one more question, after modifying the code:

: // proj2.cpp : Defines the entry point for the console application.
: //
:
: #include "stdafx.h"
: #include <iostream>
: #include <vector>
: #include <string>
: #include <cstdio>
:
: using namespace std;
:
: vector<string>* getTyphoon(){
: vector<string>* typhoonList = new vector<string>();
Why do you allocate your vector dynamically?
: typhoonList->push_back("A");
: return typhoonList;
: }
:
: int main(int argc, char* argv[]){
: vector<string> typhoonList = *getTyphoon();
Here, you copy the dynamically allocated vector from getTyphoon() and
lose the pointer to the original. You have produced a memory leak,
since the vector cannot be used and cannot be deleted anymore.
:
: string A = typhoonList.at(0);
: cout << "A: " << A << endl;
:
: return 0; // suppose i don't want to delete the dynamic location
: yet


Well, you couldn't, even if you wanted to, since you lost the pointer to
it.

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.