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... 7 10602
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".
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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |