![]() |
| | #1 |
| Registered User Join Date: Sep 2007
Posts: 32
| problems with stl vectors function push_back 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;
};
Any ideas? |
| _lazycat_ is offline | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #6 | |
| Registered User Join Date: Dec 2006
Posts: 1,780
| Quote:
| |
| cyberfish is offline | |
| | #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 | |
| | #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 | |
| | #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:
| |
| cyberfish is offline | |
| | #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 | |
| | #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; 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 | |
![]() |
| Tags |
| class, push_back, vector |
| Thread Tools | |
| Display Modes | |
|
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 |