Connecting Tech Pros Worldwide Help | Site Map

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

Forecast
Guest
 
Posts: n/a
#1: Jul 22 '05
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...


Jeff Schwab
Guest
 
Posts: n/a
#2: Jul 22 '05

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


Forecast wrote:[color=blue]
> 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...
>
>[/color]

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".


Jack Klein
Guest
 
Posts: n/a
#3: Jul 22 '05

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


On Sat, 3 Jan 2004 11:51:49 +0800, "Forecast" <forecast@csis.hku.hk>
wrote in comp.lang.c++:
[color=blue]
> 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.[/color]

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.
[color=blue]
> 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>[/color]

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.
[color=blue]
> :
> : 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);[/color]

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());
[color=blue]
> :
> : 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...[/color]

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
Forecast
Guest
 
Posts: n/a
#4: Jul 22 '05

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


thx.


Forecast
Guest
 
Posts: n/a
#5: Jul 22 '05

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


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>[color=blue]
>,std::basic_string<char,std::char_traits<[/color]
: 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>[color=blue]
>,std::basic_string<char,std::char_traits<char>,[/color]
: 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>[color=blue]
>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato[/color]
r<char> > >
:[color=blue]
>::vector<std::basic_string<char,std::char_traits< char>,std::allocator<char>
>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato[/color]
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>[color=blue]
>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato[/color]
r<char> > >
:[color=blue]
>::vector<std::basic_string<char,std::char_traits< char>,std::allocator<char>
>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato[/color]
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>[color=blue]
>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato[/color]
r<char> > >
:[color=blue]
>::~vector<std::basic_string<char,std::char_traits <char>,std::allocator<char
>
>,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocato[/color]
r<char> > > >' : identifier was truncated to '255' characters in the debug
information
: Linking...
:
: proj5.exe - 0 error(s), 5 warning(s)

"Jack Klein" <jackklein@spamcop.net> wrote in message
news:4ahcvvc2d7ajhs7tdu7uh66omir04leqsh@4ax.com...[color=blue]
> On Sat, 3 Jan 2004 11:51:49 +0800, "Forecast" <forecast@csis.hku.hk>
> wrote in comp.lang.c++:
>[color=green]
> > 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.[/color]
>
> 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.
>[color=green]
> > 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>[/color]
>
> 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.
>[color=green]
> > :
> > : 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);[/color]
>
> 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());
>[color=green]
> > :
> > : return 0;
> > : }
> >
> >
> > (2) I tried to compile the second code in UNIX again... (that's using[/color][/color]
the[color=blue][color=green]
> > "printf" and removal of #include "stdafx.h"), the following error is[/color][/color]
out,[color=blue][color=green]
> > 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...[/color]
>
> 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[/color]


Samuele Armondi
Guest
 
Posts: n/a
#6: Jul 22 '05

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


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



Forecast
Guest
 
Posts: n/a
#7: Jul 22 '05

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


thx.

M$ no good.

"Samuele Armondi" <sammyboyuk_NOSPAM_@hotmail.com> wrote in message
news:3ff6eb65_2@mk-nntp-1.news.uk.worldonline.com...[color=blue]
> 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
>
>
>[/color]


Rolf Magnus
Guest
 
Posts: n/a
#8: Jul 22 '05

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


Forecast wrote:
[color=blue]
> 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>();[/color]

Why do you allocate your vector dynamically?
[color=blue]
> : typhoonList->push_back("A");
> : return typhoonList;
> : }
> :
> : int main(int argc, char* argv[]){
> : vector<string> typhoonList = *getTyphoon();[/color]

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.
[color=blue]
> :
> : string A = typhoonList.at(0);
> : cout << "A: " << A << endl;
> :
> : return 0; // suppose i don't want to delete the dynamic location
> : yet[/color]

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

Closed Thread