problems with stl vectors function push_back

This is a discussion on problems with stl vectors function push_back within the C++ Programming forums, part of the General Programming Boards category; hi all, I am currently trying to write a code that involves appending a value to the end of a ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    32

    problems with stl vectors function push_back

    hi all,

    I am currently trying to write a code that involves appending a value to the end of a vector, but i am having trouble.

    Code:
    TextNumerical TextLiteral::convertToTextNum()
    {
        TextNumerical array;
    
        for ( int i=0; i<getLength(); i++ )
        {
            if ( !isLetter ( text[i] ) )
            {
                continue;
            }
    
            UINT a = (UINT) text[i] - 65;
    
            array.push_back ( a );
        }
    
        return array;
    }
    
    
    
    
    
    class TextNumerical
    {   // TextNumerical is a structure with two arrays -- an input
                            // and an output.The data has been converted into numbers
                            // for encoding and decoding.
    private:
        std::vector<UINT> input;
        std::vector<UINT> output;
    
    public:
        TextNumerical() {};
        TextNumerical( std::vector<UINT> );
    
        void push_back ( const UINT& val ) { output.push_back ( val ); }
    
        std::vector<UINT> getOutput() const { return output; }
        std::vector<UINT> getInput() const { return input; }
        int getSize() const { return input.size(); }
        void reserve( std::string::size_type size ) { output.reserve( size ); }
    
        void printOutput() const;
    
        std::string convertToText() const;
    };
    It will compile and run okay without any errors, but for some reasons, array.pushback(a) won't change my object. I've also tried overloading operator[], but to no avail.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,247
    Either step through with a debugger, or place a line of output into TextNumerical::push_back(). My guess is that you will find it is never being called: never calling a function is one way to guarantee it has no effect.

    From the code you've given, either getLength() is returning zero or isLetter(text[i]) is always yielding true.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    Thanks for your quick reply, but I did step it through with a debugger, and it was called. I also checked that value of a to make sure it is valid, but for some strange bizarre reason, it simply didn't work...

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    Can you post the minimum compilable code that shows this?

    I don't see anything wrong with the code posted.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    typedef unsigned int UINT;
    class TextNumerical
    {
    private:
        std::vector<UINT> input;
        std::vector<UINT> output;
    
    public:
        void push_back ( const UINT& val ) { output.push_back ( val ); }
        int getSize() const { return input.size(); }
        void printOutput() const;
        std::string convertToText() const;
    };
    class TextLiteral
    {
    private:
        std::string text;
    public:
        TextLiteral( std::string input):text(input){};
        int getLength() const { return text.length(); }
        TextNumerical convertToTextNum();
    };
    void TextNumerical::printOutput() const
    {
        for ( int i=0; i<input.size(); i++ )
        {
            std::cout << output[i] << " ";
        }
    
        std::cout << std::endl;
    }
    int main ()
    {
                TextLiteral plain( "FOO" );
                TextNumerical plainTN( plain.convertToTextNum() );
    plainTN.printOutput();
    return 0;
    }
    
    TextNumerical TextLiteral::convertToTextNum()
    {
        TextNumerical array;
        for ( int i=0; i<getLength(); i++ )
        {
            UINT a = (UINT) text[i] - 65;
            array.push_back ( a );
        }
        return array;
    }

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    Code:
        for ( int i=0; i<input.size(); i++ )
        {
            std::cout << output[i] << " ";
        }
    Also, fix your indentation.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    Side note:

    The design of your classes seem a bit strange... what are you trying to do?

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    Thanks. I fixed it, but it didn't change the results as that function only affect the output, but it was array.push_back that ran without error but didn't work. The indentations are due to me deleting a *LOT* of blocks and adding random stuffs and being too lazy to fix them. And the weirdness of the classes are probably once again, due to me deleting functions and members that are irrelevant. The program is supposed to be encode a text, and the functions above are supposed to transcode between A B C D... to 0 1 2 3...

    Hope it make sense.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    Changing input.size() to output.size() works for me (assuming it's doing what I think it should be doing...).

    The program is supposed to be encode a text, and the functions above are supposed to transcode between A B C D... to 0 1 2 3...
    In that case, I would suggest only storing the data in a "raw" form. So your input functions would convert input in (potentially) different formats to the internal form, and your output functions would convert it from the raw form to the desired output format. This way, other parts of the class only need to be written to work on the internal form.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    Thanks for your help! I realised that I changed the wrong bits...it works now.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, this code is ugly & unportable:
    Code:
    UINT a = (UINT) text[i] - 65;
    It's ugly because it uses a magic number (i.e. 65) and it's unportable because it will only work on ASCII systems.
    Better ways would be to use strtoul() or std::stringstream.
    Last edited by cpjust; 06-05-2009 at 02:56 PM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21