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

F

Forecast

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

Jeff Schwab

Forecast said:
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".
 
J

Jack Klein

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

Forecast

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 said:
,std::basic_string<char,std::char_traits<
: char>,std::allocator<char> > const
&,std::basic_string<char,std::char_traits<char>,std::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 said:
,std::basic_string<char,std::char_traits<char>,
: std::allocator<char> >
&,std::basic_string<char,std::char_traits<char>,std::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 said:
,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocato
r said:
::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 said:
,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocato
r said:
::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 said:
,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocato
r said:
::~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 said:
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.
 
S

Samuele Armondi

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
 
R

Rolf Magnus

Forecast said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top