|
Hello everybody:
I was wondering if ther is a way to use the cin.getline() function when you use string objects. With the code snipet below I get the following error:
error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int,char)' : cann
ot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
hwproject.exe - 1 error(s), 0 warning(s)
I always encounter this problem and I don't know what to do to solve this. Is there a better way to do this with string objects? Can you tell me what I did wrong with the code below? Any answer is more than welcome.
#include <string><br />
#include <iostream><br />
using namespace std;<br />
<br />
void main()<br />
{<br />
int itemNum;<br />
string Desc;<br />
cout << "Enter Item Number: ";<br />
cin >> itemNum;<br />
cout << "Enter Description: ";<br />
cin.getline(Desc, sizeof(Desc), (char)"\n");<br />
}
Luis E. Cuadrado
)
|
|
|
|
|
One solution is std::getline().
string sInput;
std::getline(cin, sInput);
Kuphryn
|
|
|
|
|
Thanks for replying Kuphryn. I gave it a try and the program complies but when I run it, the variable where I store the value using the std::getline() function doesn't show up when I use cout.
#include <iostream>
#include <string>
using namespace std;
void main()
{
string itemID;
string Desc;
cout << "Enter ID: ";
cin >> itemID;
cout << "Enter Description: ";
getline(cin, Desc);
cout << "itemID: " << itemID << endl;
cout << "Desc: " << Desck << endl;
}
This is the output that I got.
Enter ID: 222
Enter Description: matches
itemID: 222
Desc:
I'm asuming that there are some kind of garbage collecting thing that I have to do here, is it? Let me know what you think.
Thanks for your help,
Luis E.
Luis E. Cuadrado
)
|
|
|
|
|
Hola Luis,
There are two problems, one of them is not your fault:std::getline is broken in MSVC++ 6.0. See here[^] for a fix.
- (this assumes you've installed the previous fix) After reading the
int , there are extra characters pending to be read in the first line (at least, the terminating '\n' ): eat this before going for the description:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string itemID;
string Desc;
cout << "Enter ID: ";
cin>>itemID;
cout << "Enter Description: ";
getline(cin, Desc);
getline(cin, Desc);
cout << "itemID: " << itemID << endl;
cout << "Desc: " << Desc << endl;
}
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hola Joaquín,
Thank you for your response. I did exactly what you told me and it works. Thank you for pointing me out to that Microsoft site and for your help.
Another question that I have is, if I want to do that in a loop. Do I need to flush or wipe-out the stream that I'm using? The reason that I'm asking is that I put everything in a loop except the cout statements and the loops keeps going and it doesn't stop. I step over the code and I noticed that there still data, aparently in the stream. Have you experience this before?
Thanks,
Luis E.
Luis E. Cuadrado
)
|
|
|
|
|
I have the STL distributed with VC6, but I need a fixed version that doesn't leak memory and resources like that one does.
Does anyone know of any other (preferably free) replacement libraries?
I have tried STLPort (which improved resource leask but increased memory leaks) and I have tried SGI, but that wouldn't compile. Any other ideas?
Derek Lakin.
I wish I was what I thought I was when I wished I was what I am.
Salamander Software Ltd.
|
|
|
|
|
Derek Lakin wrote:
need a fixed version
Do you have tried the fixes from Dinkumware[^]?
They also sell a STL, I think. But I have never tried that.
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
Thanks for the link . Sometimes you can overlook the most obvious solutions
I'll try it out and let you know how I get on.
Derek Lakin.
I wish I was what I thought I was when I wished I was what I am.
Salamander Software Ltd.
|
|
|
|
|
I tried all of the fixes and it made things worse, so I used all the fixes apart from the stream-related ones and it improved the problem drastically.
Still not all fixed though
Derek Lakin.
I wish I was what I thought I was when I wished I was what I am.
Salamander Software Ltd.
|
|
|
|
|
What type of problems do you have? Where are you leaking memory?
For me, std::string seems sometimes to have those problems, but it has never been so bad that I have to fix it, that is, you can't see it in 'Task Manager', but VC++6 complains.
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
The problems I had experienced were mainly danlging pointer errors.
Since first trying the fixes I have since removed those related to streams and have now managed to completely erradicate any errors
I'm just left now with 2 small memory leaks (1,864 bytes), which aren't STL related, and 16 resource leaks that aren't really leaks, it's just that DevPartner Studio picks them up on application exit, but they get freed when the MFC libraries are unloaded.
Thanks for your help
Derek Lakin.
I wish I was what I thought I was when I wished I was what I am.
Salamander Software Ltd.
|
|
|
|
|
Derek Lakin wrote:
The problems I had experienced were mainly danlging pointer errors.
ahh.. BoundsChecker complaints? i'm not sure those problems are actually problems. BC complains about a lot of stuff that isn't a real problem.
-c
Image tools: ThumbNailer, Bobber, TIFFAssembler
|
|
|
|
|
Derek Lakin wrote:
which improved resource leask but increased memory leaks
Depending on a define STLPORT will hold a chunk of memory until the progam ends to improve the speed of allocations. This will look to memory leak tooks as a real leak but it is not. There are a couple of discussions in their forum about this topic. Here is one: http://www.stlport.com/dcforum/DCForumID6/1181.html[^]
John
|
|
|
|
|
**[EDIT] Figured out how to do it myself. [/EDIT]**
Hey, I was looking at the IE Deskband Tutorial and it shows how to add a button to your deskband. But I am not sure how to add another button to this, can anyone help me out? THanks.
Here is what is in the tutorial:
TCHAR* pCaption = _T("Get Quote");<br />
int iIndex = ::SendMessage(m_hWnd, TB_ADDSTRING, 0,(LPARAM)pCaption);<br />
<br />
HICON hMotley = LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_MOTLEY));<br />
m_hImageList = ImageList_Create(16,16, ILC_COLOR16, 1, 0);<br />
int iImageIndex = ImageList_AddIcon(m_hImageList, hMotley);<br />
DestroyIcon(hMotley);<br />
::SendMessage(m_hWnd, TB_SETIMAGELIST, 0, (LPARAM)m_hImageList);<br />
<br />
TBBUTTON Button;<br />
ZeroMemory((void*)&Button, sizeof(TBBUTTON));<br />
Button.idCommand = IDM_GETQUOTE;<br />
Button.fsState = TBSTATE_ENABLED;<br />
Button.fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_SHOWTEXT;<br />
Button.dwData = 0;<br />
Button.iString = iIndex;<br />
Button.iBitmap = 0;<br />
::SendMessage(m_hWnd, TB_INSERTBUTTON, 0, (LPARAM)&Button);<br />
|
|
|
|
|
I have a calss that have a member variable of type ofstream, I would like to initialize this variable in constructor.
MyClass
{
MyClass(ofstream& Logger) { m_Logger = Logger; }
private:
ofstream m_Logger;
}
But seems like there is no assignment operator for ofstream.
Any idea how can i do this?
|
|
|
|
|
Anonymous wrote:
MyClass<br />
{<br />
MyClass(ofstream& Logger) { m_Logger = Logger; }<br />
<br />
private:<br />
ofstream m_Logger;<br />
}
Why not using
MyClass<br />
{<br />
MyClass(ofstream& Logger) { m_Logger = Logger; }<br />
<br />
private:<br />
ofstream& m_Logger;<br />
}
instead ... Using a reference is even faster than creating a new stream...
Greeting, Ralph
|
|
|
|
|
Yep, ofstream s cannot be copied. So, you have several alternatives:
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Can anyone tell me about the easiest way to handle events that are fired from ATL dll ? I used ActiveX controls and when we developer client application in MFC, the event handling mechanism is very easy and automatically done by the framework. But this is not the case if our COM server is not an ActiveX but a dll. Can anyone point me out some sample app for sinking connection points in MFC ???
|
|
|
|
|
I have a vector of pointers vector <CPersons*> that is passed in a function by reference.
This function has access to another similar vector say m_Persons and i have to copy all entries from m_Persons to Persons
MyClass::MyFunc(vector<CPersons*>& Persons)
{
//This class has a member function vector<CPersons*> m_Persons
// i need to copy content of m_Persons into Persons
if (Persons.capacity() < Persons.size()+m_Persons.size())
Persons.resize(Persons.size()+m_Persons->size());
copy(m_Persons.begin(), m_Persons.end(), Persons.begin() );
}
but since this is a vector of pointer, my understanding is that anytime re-allocation of
destination vector Persons is needed, all these pointer would become invalid.
Is my understanding correct?
If yes, is this best of way doing it. If no, please advise how to attempt such problem.
|
|
|
|
|
this is probably ok. your understanding is incorrect - you will end up with two vectors containing the list of pointers. the only thing you need to be careful of is who does the clean up i.e. make sure the pointed-to objects don't get destroyed twice.
your resizing test is wrong though. capacity() returns the amount of space *allocated*, not necessarily *used*. you can just do:
<br />
Persons.resize( m_Persons.size() ) ;<br />
your misunderstanding is probably this: if you resize a vector, the memory used to store that vector's elements may be moved i.e. the memory used to store your pointers may change but not the pointers themselves.
Software is everything. It also sucks. Charles Fishman [^]
Awasu 1.0.2 (beta)[^]: A free RSS reader with support for Code Project.
|
|
|
|
|
Anonymous wrote:
anytime re-allocation of
destination vector Persons is needed, all these pointer would become invalid
No. They get copied, but still point to the same chunk of memory containing the person-object.
But who is responsible to cleaning up, e.g. calling 'delete'?
Using smart pointer can help you here - but NEVER EVER use std::auto_ptr!!!!
Try the shared_ptr from www.boost.org[^], it uses reference counting to delete the obect it points to when the last ptr pointing to it goes out of scope.
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
I have a vector of pointers like
vector<cperson*> Persons
This vector is passed to method func by reference
This func has another vector of CPerson* m_Persons and i have to copy all entries from m_Persons to Persons
I am doing it in following way
func(vector<cperson*>& Persons)
{
if (Persons.capacity() < Persons.size()+m_Persons->size())
Persons.resize(Persons.size()+m_Persons->size());
copy(m_Persons.begin(), m_Persons.end(), Persons.begin() );
}
but seems to me doing this way i an losing some references CPerson* in Persons
Is this right way of doing this?
Am i in problem because anytime vector need to reallocated memory, i am loosing all previous references?
PS: Please oppologize any typos as i am only trying to give the concetp
|
|
|
|
|
What i wanted to say in first two lines is
I have a vector of pointers of a class Persons
vector<persons*>
|
|
|
|
|
Has anyone here used CMimeMessage to compose or decode a MIME encoded message and attachments etc?
Hush,hush...
thought I heard you call my name now.
Kula Shaker.
Amit Dey
Latest articles at CP - PocketPC New menu
Office addin
|
|
|
|
|
Yep, I did. But just some small tests, if and how it works. No real app.
|
|
|
|