C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-05-2009, 06:13 AM   #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?
_lazycat_ is offline   Reply With Quote
Old 06-05-2009, 06:40 AM   #2
Registered User
 
Join Date: Jun 2005
Posts: 1,343
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%.
grumpy is offline   Reply With Quote
Old 06-05-2009, 06:44 AM   #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...
_lazycat_ is offline   Reply With Quote
Old 06-05-2009, 07:27 AM   #4
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Can you post the minimum compilable code that shows this?

I don't see anything wrong with the code posted.
cyberfish is offline   Reply With Quote
Old 06-05-2009, 08:03 AM   #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;
}
_lazycat_ is offline   Reply With Quote
Old 06-05-2009, 08:06 AM   #6
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Quote:
Code:
    for ( int i=0; i<input.size(); i++ )
    {
        std::cout << output[i] << " ";
    }
Also, fix your indentation.
cyberfish is offline   Reply With Quote
Old 06-05-2009, 08:09 AM   #7
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Side note:

The design of your classes seem a bit strange... what are you trying to do?
cyberfish is offline   Reply With Quote
Old 06-05-2009, 08:25 AM   #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.
_lazycat_ is offline   Reply With Quote
Old 06-05-2009, 08:29 AM   #9
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Changing input.size() to output.size() works for me (assuming it's doing what I think it should be doing...).

Quote:
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.
cyberfish is offline   Reply With Quote
Old 06-05-2009, 08:44 AM   #10
Registered User
 
Join Date: Sep 2007
Posts: 32
Thanks for your help! I realised that I changed the wrong bits...it works now.
_lazycat_ is offline   Reply With Quote
Old 06-05-2009, 02:53 PM   #11
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
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.
__________________
"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

Last edited by cpjust; 06-05-2009 at 02:56 PM.
cpjust is offline   Reply With Quote
Reply

Tags
class, push_back, vector

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Library Files for these unreferenced functions? lehe C++ Programming 3 01-31-2009 10:30 PM
Including lib in a lib bibiteinfo C++ Programming 0 02-07-2006 02:28 PM
Game Pointer Trouble? Drahcir C Programming 8 02-04-2006 02:53 AM
C++ compilation issues Rupan C++ Programming 1 08-22-2005 05:45 AM
Please Help - Problem with Compilers toonlover C++ Programming 5 07-23-2005 10:03 AM


All times are GMT -6. The time now is 06:07 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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