vector<CString> *pvecString ?? :confused:
-
Hello, I'm trying the following code in an MFC project, which is causing a compiler error. I hope you can guide me with any error/mistake in my code: My Code:
... #include <MyClass.h> #include <vector> ... vector<CString> *g_pvecStrings; ... void g_MyOtherFunction(void); // Global Function Prototype ... void CMyClass::MyFunction(void) { vector<CString> vecStrings; ... /// Both the statements below are causing different compiler errors. g_pvecStrings = vecStrings; // :confused: // (1) // g_pvecStrings = &vecStrings; // :~ ... g_MyOtherFunction(); ... vecStrings.clear(); } void g_MyOtherFunction(void) { CString csMyStr; ... // Filling the csMyStr g_pvecStrings->push_back(csMyStr); ... }
The MSVC++ 6.0 (SP5) compiler error for the statement marked as (1) above:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::vector<class CString,class std::allocator<class CString> >' (or there is no acceptable conversion)
Please reply on this... Thanks, Rgds, Nirav Doshi * Don't wish it was easier, wish you were better! *
MFC classes like CString tend to not play nicely with STL components. Pick one or the other, and try not mix them without some kind of adapter.
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
-
MFC classes like CString tend to not play nicely with STL components. Pick one or the other, and try not mix them without some kind of adapter.
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
Thanks Andrew for your reply! Actually, I have my own
CWBString
class which basically is wrapping string data, which is (obviously) as a "char *
". I changed that class references withCString
for the purpose of the query post for easy explanation. If as you're saying, I guess I won't be able to use thevector
container! :doh:... Is that it? X| Thanks, Rgds, Nirav * Don't wish it was easier, wish you were better! * -
Hello, I'm trying the following code in an MFC project, which is causing a compiler error. I hope you can guide me with any error/mistake in my code: My Code:
... #include <MyClass.h> #include <vector> ... vector<CString> *g_pvecStrings; ... void g_MyOtherFunction(void); // Global Function Prototype ... void CMyClass::MyFunction(void) { vector<CString> vecStrings; ... /// Both the statements below are causing different compiler errors. g_pvecStrings = vecStrings; // :confused: // (1) // g_pvecStrings = &vecStrings; // :~ ... g_MyOtherFunction(); ... vecStrings.clear(); } void g_MyOtherFunction(void) { CString csMyStr; ... // Filling the csMyStr g_pvecStrings->push_back(csMyStr); ... }
The MSVC++ 6.0 (SP5) compiler error for the statement marked as (1) above:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::vector<class CString,class std::allocator<class CString> >' (or there is no acceptable conversion)
Please reply on this... Thanks, Rgds, Nirav Doshi * Don't wish it was easier, wish you were better! *
First line you're trying to assign a std::vector<T> to a std::vector<T>*, which obviously fails, and should fail unless something really fishy is at work here. Second line you're trying to assign a local variable to a global variable by using the address-of operator on the local variable. This will cause the g_pvecStrings variable to point to memory on an unwinded stack after the termination of the void CMyClass::MyFunction(void) method call. The correct approach would be to do: *g_pvecStrings = vecStrings; presuming g_pvecStrings != 0. An even more correct approach would be an application redesign, but that's probably just the C++ purist in me talking. ;) Hope that helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/)
-
First line you're trying to assign a std::vector<T> to a std::vector<T>*, which obviously fails, and should fail unless something really fishy is at work here. Second line you're trying to assign a local variable to a global variable by using the address-of operator on the local variable. This will cause the g_pvecStrings variable to point to memory on an unwinded stack after the termination of the void CMyClass::MyFunction(void) method call. The correct approach would be to do: *g_pvecStrings = vecStrings; presuming g_pvecStrings != 0. An even more correct approach would be an application redesign, but that's probably just the C++ purist in me talking. ;) Hope that helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/)
Thanks Henrik for your reply! Henrik Stuart wrote: First line you're trying to assign a std::vector to a std::vector*, which obviously fails, and should fail unless something really fishy is at work here. The correct approach would be to do: *g_pvecStrings = vecStrings; presuming g_pvecStrings != 0. The
*g_pvecStrings = vecStrings;
doesn't work too! :( Henrik Stuart wrote: An even more correct approach would be an application redesign, but that's probably just the C++ purist in me talking. Thanks for this point! My basic purpose is to call a global function which is going to fill the vector with strings. The filled vector is to be used in two of my class member functions. So to pass the data between the class member functions and the global functions I was doing this. So, I had the global pointer to a vector, which was to point to a vector which will be allocated on the stack within my member function. From within the SAME function I was releasing and making theg_pvecStrings = NULL
; Please suggest the right way to get this going! Thanks! * Don't wish it was easier, wish you were better! * -
Thanks Henrik for your reply! Henrik Stuart wrote: First line you're trying to assign a std::vector to a std::vector*, which obviously fails, and should fail unless something really fishy is at work here. The correct approach would be to do: *g_pvecStrings = vecStrings; presuming g_pvecStrings != 0. The
*g_pvecStrings = vecStrings;
doesn't work too! :( Henrik Stuart wrote: An even more correct approach would be an application redesign, but that's probably just the C++ purist in me talking. Thanks for this point! My basic purpose is to call a global function which is going to fill the vector with strings. The filled vector is to be used in two of my class member functions. So to pass the data between the class member functions and the global functions I was doing this. So, I had the global pointer to a vector, which was to point to a vector which will be allocated on the stack within my member function. From within the SAME function I was releasing and making theg_pvecStrings = NULL
; Please suggest the right way to get this going! Thanks! * Don't wish it was easier, wish you were better! *Nirav Doshi wrote: Thanks for this point! My basic purpose is to call a global function which is going to fill the vector with strings. The filled vector is to be used in two of my class member functions. So to pass the data between the class member functions and the global functions I was doing this. The C++ way is to avoid using anything global and use a static member function instead and static class variable. John
-
Nirav Doshi wrote: Thanks for this point! My basic purpose is to call a global function which is going to fill the vector with strings. The filled vector is to be used in two of my class member functions. So to pass the data between the class member functions and the global functions I was doing this. The C++ way is to avoid using anything global and use a static member function instead and static class variable. John
Oh! Thanks! :))... With my confusion to trying to get my work done within my timeline, I delved into some of my old C-style coding! :( I guess, this would be the best way to solve my requirement! Thanks again! Rgds, Nirav * Don't wish it was easier, wish you were better! *
-
Nirav Doshi wrote: Thanks for this point! My basic purpose is to call a global function which is going to fill the vector with strings. The filled vector is to be used in two of my class member functions. So to pass the data between the class member functions and the global functions I was doing this. The C++ way is to avoid using anything global and use a static member function instead and static class variable. John
Making the variable a class static does little to the design. It just changes the namespace. If it was bad code as a global variable, it is still bad code as a class static. My question is why can't the vector be passed by address or reference to the global functions. Then you avoid the global variables, true or class statics. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Making the variable a class static does little to the design. It just changes the namespace. If it was bad code as a global variable, it is still bad code as a class static. My question is why can't the vector be passed by address or reference to the global functions. Then you avoid the global variables, true or class statics. Tim Smith I'm going to patent thought. I have yet to see any prior art.
1. Filling the vector through the global or static pointer isn't a good idea. You should pass the vector to the
MyOtherFunction
by reference.void CMyClass::MyOtherFunction(vector& vec) { vec.push_back(...) }
2. If the compiler generates error on*g_pvecStrings = vecStrings
, I think that your classCWBString
is the deliquent (because withCString
it works ok). The assignment operator in vector causes assignments of each element. Hence, did you declared correctly the copy constructor or assignment operator inCWBString
? Robert-Antonio "Science is a differerntial equation. Religion is a boundary condition." -
1. Filling the vector through the global or static pointer isn't a good idea. You should pass the vector to the
MyOtherFunction
by reference.void CMyClass::MyOtherFunction(vector& vec) { vec.push_back(...) }
2. If the compiler generates error on*g_pvecStrings = vecStrings
, I think that your classCWBString
is the deliquent (because withCString
it works ok). The assignment operator in vector causes assignments of each element. Hence, did you declared correctly the copy constructor or assignment operator inCWBString
? Robert-Antonio "Science is a differerntial equation. Religion is a boundary condition."Thanks Robert for your reply! :) Robert A. T. Káldy wrote: 1. Filling the vector through the global or static pointer isn't a good idea. You should pass the vector to the MyOtherFunction by reference. I have been trying EXACTLY that now, but it is cribbing about
xutility
:c:\program files\microsoft visual studio\vc98\include\xutility(39) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const class CWBString' (or there is no acceptable conversion) c:\program files\microsoft visual studio\vc98\include\vector(170) : see reference to function template instantiation 'void __cdecl std::fill(class CWBString *,class CWBString *,const class CWBString &)' being compiled
:~ Robert A. T. Káldy wrote: 2. If the compiler generates error on *g_pvecStrings = vecStrings, I think that your class CWBString is the deliquent (because with CString it works ok). The assignment operator in vector causes assignments of each element. Hence, did you declared correctly the copy constructor or assignment operator in CWBString? I am using
CWBString
extensively in my whole project and it has a (deep) copy constructor, as well as almost ALL the operators overloaded for standards as well as varied purposes (varied like*=
forIsSimilar()
in which I check for similar strings). So I suppose my problem was that I was trying to use STL containers like normal pointers which can be shallow copied! I hope I'm correct here... :) Thanks again! Nirav * Don't wish it was easier, wish you were better! * -
Making the variable a class static does little to the design. It just changes the namespace. If it was bad code as a global variable, it is still bad code as a class static. My question is why can't the vector be passed by address or reference to the global functions. Then you avoid the global variables, true or class statics. Tim Smith I'm going to patent thought. I have yet to see any prior art.
Tim Smith wrote: Making the variable a class static does little to the design. It just changes the namespace. If it was bad code as a global variable, it is still bad code as a class static. I disagree. There are legitimate reasons to have class static variables and they are definitly not as bad as globals as you can make them private or protected. If the same data will be shared for all instances of an object it makes no sense to store a pointer to the data in each instance. John