Help needed with C++ class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 12 of 12

Thread: Help needed with C++ class

Threaded View

  1. #1
    Join Date
    May 2002

    Help needed with C++ class

    I wrote a little C++ class to implement the parsing of text into matrices. I works pretty well except that I cant seem to access the string vector that holds the numeric data as strings. It doesnt make sense to me because I can demonstrate that in at least one function of the class, the string vector is properly initialized. But when I try to acces it in another class function (the retrieval function), the vector has size = 0.

    Heres some pseudo-code to further explain my dilemma:

    // lex.h
    #include <string>
    #include <vector>
    using namespace std;
    class CLex
     CLex(){ m_vs.resize(0);}
     virtual ~CLex(){}
     vector<string> m_vs;
     void parse(string s) 
     { // ... 
        m_vs.push_back("numeric string..."); 
     vector<string> get_svec() { return m_vs; }
    The problem here is that the member vector, m_vs gets properly initialized in the CLex:arse function, but when I try to use CLex::get_svec() to access the data, it comes up empty. Checking the value(s) of m_vs in the function get_svec, m_vs is empty. So what happened to it?

    #include "lex.h"
    int main()
        string s;
        vector<string> vs;
        CLex lx;
        s = "1 2 3\r\n4 5 6\r\n";
        vs = lx.get_svec() ;
        cout << vs[0] << endl;
    The class member vector, m_vs, will have values of "1", "2", "3", "4", "5", "6" within the CLex:arse() function, but will be empty (have zero size) in the CLex::get_svec() function.

    Can anyone show me what I am doing wrong here? I'm sure it's something fairly simple and stupid as usual.


    Last edited by Mike Pliam; December 12th, 2003 at 01:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)