472,980 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

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

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
7 10568
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
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
thx.
Jul 22 '05 #4
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sid | last post by:
Hi, I am trying to run this simple C++ code. I am stuck at the DOS prompt after entering in text as the normal commands like Ctrl-Z or Ctrl-D used for EOL don't seem to work. I am using...
0
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
4
by: Gama Franco | last post by:
Hi, I've been developing this API, but now I get stuck in a compiling error and I'm out of ideas. Some comments are welcome. The hierarchy is bases in three classes, and I will explain it...
2
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
2
by: Buck Brown | last post by:
Hi, I am using Visual C++ 6.0 MFC. I have been trying to build an array of objects. First I tried using CArray but it was just giving me fits. Once I got my copy contructor built so I would not...
6
by: Rahul K | last post by:
Hi I am working on Visual Studio on Windows. I have a function which return the list of all the IP Addresses of a machine: vector<char *getAllLocalIPAddress() { char localHostName; struct...
5
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The...
8
by: Mike Jolley | last post by:
Hello First off, I'm a student so I'm pretty new to C++, and therefore I have probably made a stupid mistake somewhere. Anyway Ive been trying to fix this 5 hours straight now, so i need a...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.