Thread: Classe constructor and Vector question

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Classe constructor and Vector question

    I was wondering how to declare a vector into a constructor. Can someone kindly tell me where I am going wrong?

    Code:
    class WordRecord
    {
    public:
    
    	WordRecord(vector<string> &vs, LineRecord * & pLineRec);


    I am not sure about the declarations here:

    Code:
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
    	 vectorString[i]= vs[i];
    	*pLineRec = *p;
    }
    This is to create a new Word Record, which brings in a Line Record (I am also wondering if I am bringing in the Line Record properly):

    Code:
      LineRecord * pLineRec;
      WordRecord * pWordRec;
    
      vector<string> vs[80];
      vs[i].push_back(line);
    
      pLineRec = new LineRecord(sentence, filename, lineNum);
    
      pWordRec = new WordRecord(vs[i], pLineRec);
    In any case, I can't seem to be able to bring in the vector into the Word Record. I know it's because I can't declare my constructor properly. Any help would be much appreciated.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
    	 vectorString[i]= vs[i];    // where is 'i' declared?
    	*pLineRec = *p;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Oops, my bad. I cut too much out.

    Code:
      
      LineRecord * pLineRec;
      WordRecord * pWordRec;
    
      vector<string> vs[80];
      vs[i].push_back(line);
    
      pLineRec = new LineRecord(sentence, filename, lineNum);
    
      pWordRec = new WordRecord(vs[i], pLineRec);
    In any case that's where it is declared. It's used to insert words into the vector.
    Last edited by stimpyzu; 03-28-2004 at 12:17 AM.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    ....
    vector<string> vs[80];
    vs[i].push_back(line);   // what is the current value of variable i?
                                          // where is it initialized?
    ....
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by stimpyzu
    It's initialized in the same function and it's int i = 0.
    How about i on this one?
    Code:
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
    	 vectorString[i]= vs[i];    // where is 'i' declared?
    	*pLineRec = *p;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    In WordRecord::WordRecord
    Last edited by stimpyzu; 03-28-2004 at 12:32 AM.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Could you just post entire code for class WordRecord?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    That would make sense... but don't laugh, I'm new at classes

    Code:
    class WordRecord
    {
    public:
    
    	static WordRecordIndex index;
    
    	WordRecord();
    
    	WordRecord(vector<string> &vs, LineRecord * & pLineRec);
    
    	string find(WordRecord &key);
    
    	bool operator >(const WordRecord & rhs) const;
    	bool operator ==(const WordRecord & rhs) const;
    	bool operator >=(const WordRecord & rhs) const {return ((*this) > rhs ) || ((*this) == rhs );}
    	bool operator <(const WordRecord & rhs)  const {return !((*this) >= rhs );}
    	bool operator <=(const WordRecord & rhs) const {return !((*this) > rhs );}
    	bool operator !=(const WordRecord & rhs) const {return !((*this) == rhs );}
    	friend ostream& operator << (ostream& ostr, const WordRecord& p);
    
      
    private:
    
    	vector<string> vs;
    	string *pLineRec;
    
    
    };
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You probably need to post the implementation also, not just the declaration. Anyways..
    Code:
    private:
    	vector<string> vs;   // if vs is class member then .... (cont'd)
    	string *pLineRec;
    };
    ......
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
    	 vectorString[i]= vs[i];    // what is vectorString?
    	*pLineRec = *p;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    You'll have to excuse me, I'm quite tired, I've been at this a long time....

    Code:
    #include "WordRecord.h"
    
    WordRecordIndex WordRecord::index = byLineRec;
    
    
    WordRecord::WordRecord()
    {
    	string key;
    
    }
    
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
    	int i;
            vs[i]= vs[i];
    	*pLineRec = *p;
    }
    
    
    bool WordRecord::operator >(const WordRecord & rhs) const
    {
    	if(index == byLineRec)
    	{
    		return vs>rhs.vs;
    	}
    	else
    	{
    		assert(index == byLineRec);
    		return pLineRec>rhs.pLineRec;
    	}
    }
    
    
    
    bool WordRecord::operator ==(const WordRecord & rhs) const
    {
    	return (vs==rhs.vs)&&(pLineRec==rhs.pLineRec);
    }
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  11. #11
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    From your code above (I don't know if you copied right)
    Code:
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
    	int i;
            vs[i]= vs[i];
    	*pLineRec = *p;
    }
    Variable i is never initialized
    Your class member has same name as the parameter (vs[i]= vs[i];), I suggest fix that
    What is this constructor supposed to do, copy a string in one vector to another? or?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Well actually, I've already tried it with


    Code:
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
            vectorString= vs;
    	*pLineRec = *p;
    }
    Where there was no variable to initialize, and that didn't work with the rest of the code either.


    To tell you the truth, I'm trying to follow the same thing I did for the Line Record which worked well. But because of the vector, it's confusing me for the constructor, and I'm a little iffy on bringing in the LineRecord into the WordRecord.

    Code:
    //The Line Record Implementation seems to work ok
    LineRecord::LineRecord(string s, string f, int l)
    {
    	sentence = s;
    	filename = f;
    	lineNum = l;
    }
    
    bool LineRecord::operator >(LineRecord & rhs)
    {
    	if(index == bySentence)
    	{
    		return sentence>rhs.sentence;
    	}
    	else
    	{
    		assert(index == byFile);
    		return filename>rhs.filename;
    	}
    }
    
    bool LineRecord::operator ==(LineRecord & rhs)
    {
    	return ((sentence==rhs.sentence)&&(filename==rhs.filename)&&(lineNum==rhs.lineNum));
    }
    
    
    ostream& operator << (ostream& ostr, const LineRecord& line)
    {
    	// a little example of C++ formated output
    	ostr<<setiosflags(ios::left);
    	ostr<<setw(10);
    	ostr << line.sentence;
    	ostr << line.filename;
    	ostr<< " [";
    	ostr << line.lineNum;
    	ostr<< "]";
    
    	return ostr;
    }

    The Word record will include a smilar ostream operator at the end once I've figured out how to declare the constructor.


    Code:
    //And the class works ok as well
    class LineRecord
    {
    public:
    	static LineRecordIndex index;
    
    	LineRecord(string s,string f,int l);
    
    	bool operator >(LineRecord & rhs);
    	bool operator ==(LineRecord & rhs);
    	bool operator >=(LineRecord & rhs) {return ((*this) > rhs ) || ((*this) == rhs );}
    	bool operator <(LineRecord & rhs)  {return !((*this) >= rhs );}
    	bool operator <=(LineRecord & rhs) {return !((*this) < rhs );}
    	bool operator !=(LineRecord & rhs) {return !((*this) == rhs );}
    	friend ostream& operator << (ostream& ostr, const LineRecord& p);
    
    private:
    	string sentence,filename;
    	int lineNum;
    };
    
    
    ostream& operator << (ostream& ostr, const LineRecord& p);
    As I've said, I'm using this Line Record code to guide me into doing the Word Record code
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    class WordRecord
    {
    public:
      WordRecord(vector<string> &vs);
    
    private:
      vector<string> vs;	//be sure this is vectorString and not vs, as in your earlier posts
    };
    That being said I'd think this would work:

    WordRecord::WordRecord(vector<string> & vs)
    {
    vectorString = vs;
    }


    Or you could use a copy constructor to initialize vecotrString with vs

    WordRecord::WordRecord(vector<string> &vs) : vectorString(vs)
    {}


    Or you could use a loop, if you want
    Code:
    WordRecord::WordRecord(vector<string>& vs)
    {
       int i;
       for(i = 0; i < vs.size(); ++i)
       {
          vectorString[i] = vs[i];
       }
    }

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Well, If this is what you mean:

    Code:
    public:
    	
    	WordRecord(vector<string> &vs, LineRecord * & pLineRec);
      
    private:
    
    	vector<string> vs;
    	string *pLineRec;
    
    
    };
    Code:
    WordRecord::WordRecord(vector<string> &vs, string *p)
    {
    	vectorString= vs;
    	*pLineRec = *p;
    
    
    }
    I had tried it before (and just re-tried it again) but with no success


    I don't know how I could use the copy constructor with the pLineRec, but I will give it a try. I have a feeling I'm going to be on this for a long while still... better get some coffee!!

    Thanks for your input, I appreciate it!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    He means this:
    Code:
    public:
    
        WordRecord(vector<string> &vs, LineRecord * & pLineRec);
      
    private:
    
        vector<string> vectorString;  
        string *pLineRec;
    
    
    };
    Code:
    WordRecord::WordRecord(vector<string> &vs, LineRecord *&p) // Cant use string 
    // here since you said it should be a LineRecord.
    {
        vectorString= vs;
        *pLineRec = *p;
    
    
    }
    with your code you tried to assign everything that was in vs which is what you called a parameter to the constructor to the vs that was defined in your class. By doing that you are just begging for weird stuff to happend. I suggest you read up on functions and some basic programming stuff before you move on with classes.
    Also that pointer to a reference or what it is in the constructor looks spooky but I dont know if that is valid or not.
    Last edited by Shakti; 03-28-2004 at 11:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I reach Protected Variables?
    By zayzay in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 06:32 AM