Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. vector<CString> *pvecString ?? :confused:

vector<CString> *pvecString ?? :confused:

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicshelptutorialquestion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nirav Doshi
    wrote on last edited by
    #1

    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! *

    A H 2 Replies Last reply
    0
    • N Nirav Doshi

      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! *

      A Offline
      A Offline
      Andrew Walker
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • A Andrew Walker

        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

        N Offline
        N Offline
        Nirav Doshi
        wrote on last edited by
        #3

        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 with CString for the purpose of the query post for easy explanation. If as you're saying, I guess I won't be able to use the vector container! :doh:... Is that it? X| Thanks, Rgds, Nirav * Don't wish it was easier, wish you were better! *

        1 Reply Last reply
        0
        • N Nirav Doshi

          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! *

          H Offline
          H Offline
          Henrik Stuart
          wrote on last edited by
          #4

          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/)

          N 1 Reply Last reply
          0
          • H Henrik Stuart

            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/)

            N Offline
            N Offline
            Nirav Doshi
            wrote on last edited by
            #5

            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 the g_pvecStrings = NULL; Please suggest the right way to get this going! Thanks! * Don't wish it was easier, wish you were better! *

            J 1 Reply Last reply
            0
            • N Nirav Doshi

              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 the g_pvecStrings = NULL; Please suggest the right way to get this going! Thanks! * Don't wish it was easier, wish you were better! *

              J Offline
              J Offline
              John M Drescher
              wrote on last edited by
              #6

              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

              N T 2 Replies Last reply
              0
              • J John M Drescher

                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

                N Offline
                N Offline
                Nirav Doshi
                wrote on last edited by
                #7

                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! *

                1 Reply Last reply
                0
                • J John M Drescher

                  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

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  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.

                  R J 2 Replies Last reply
                  0
                  • T Tim Smith

                    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.

                    R Offline
                    R Offline
                    Robert A T Kaldy
                    wrote on last edited by
                    #9

                    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 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? Robert-Antonio "Science is a differerntial equation. Religion is a boundary condition."

                    N 1 Reply Last reply
                    0
                    • R Robert A T Kaldy

                      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 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? Robert-Antonio "Science is a differerntial equation. Religion is a boundary condition."

                      N Offline
                      N Offline
                      Nirav Doshi
                      wrote on last edited by
                      #10

                      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 *= for IsSimilar() 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! *

                      1 Reply Last reply
                      0
                      • T Tim Smith

                        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.

                        J Offline
                        J Offline
                        John M Drescher
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups