List

Discussion in 'C++' started by Michael, Jan 1, 2011.

  1. Michael

    Michael Guest

    Hi
    imagine a display where new values are added at the bottom line. If a
    value gets untrue that line disappears and all below move up one line.

    So list might be the easiest to use? i need to draw something alike:


    if (8value == 1)
    {
    XPLMDrawString(white, PanelWindowLeft + 20, PanelWindowTop + 20,
    "Test8", 0, xplmFont_Basic);
    }

    if (7value == 1)
    {
    XPLMDrawString(white, PanelWindowLeft + 20, PanelWindowTop + 10,
    "Test7", 0, xplmFont_Basic);
    }

    if (9value == 1)
    {
    XPLMDrawString(white, PanelWindowLeft + 20, PanelWindowTop + 30,
    "Test9", 0, xplmFont_Basic);
    }

    etc.
    Now how could i have them dynamically moving up/down on true/untrue? ie.
    pack this into a list?
    Many thanks
    Michael
     
    Michael, Jan 1, 2011
    #1
    1. Advertising

  2. Michael

    Michael Guest

    Ok now I try:

    list<char> charList;

    char* arp = "Test1";

    if (something)
    charList.push_back(*arp);

    // Display the list
    list<char>::iterator theIterator;
    for( theIterator = charList.begin(); theIterator != charList.end();
    theIterator++ ) {
    XPLMDrawString(white, Panel1WindowLeft + 20, Panel1WindowTop - 20,
    theIterator, 0, xplmFont_Proportional);
    }


    I get the error:
    cannot convert ‘std::_List_iterator<char>’ to ‘char*’ for argument ‘4’
    to ‘void XPLMDrawString(float*, int, int,
    char*, int*, XPLMFontID)’
    Many thanks for help
     
    Michael, Jan 2, 2011
    #2
    1. Advertising

  3. On Jan 2, 1:07 pm, Michael <> wrote:
    > Ok now I try:
    >
    >      list<char> charList;
    >
    >      char* arp = "Test1";
    >
    >      if (something)
    >          charList.push_back(*arp);
    >
    >      // Display the list
    >      list<char>::iterator theIterator;
    >      for( theIterator = charList.begin(); theIterator != charList.end();
    > theIterator++ ) {
    >      XPLMDrawString(white, Panel1WindowLeft + 20, Panel1WindowTop - 20,
    > theIterator, 0, xplmFont_Proportional);
    >      }
    >
    > I get the error:
    > cannot convert std::_List_iterator<char> to char* for argument 4
    > to void XPLMDrawString(float*, int, int,
    >   char*, int*, XPLMFontID)
    > Many thanks for help


    compilers do their best to help you. The function wants a char* (a
    pointer to a char) theIterator isn't a char* it's a
    list<char>::iterator. To convert it you use the dereference operator
    that is defined for list iterators.

    XPLMDrawString(white, Panel1WindowLeft + 20, Panel1WindowTop -
    20,
    *theIterator, 0, xplmFont_Proportional);
     
    Nick Keighley, Jan 2, 2011
    #3
  4. Michael

    Michael Guest

    Yes, that would be? * theIterator

    And i get:

    - invalid conversion from ‘char’ to ‘char*’

    Thanks again
     
    Michael, Jan 2, 2011
    #4
  5. Michael

    LR Guest

    Michael wrote:
    > Ok now I try:
    >
    > list<char> charList;


    Please consider.

    char c = 'a'; // c is a variable of type char
    // it can contain a value of one char.

    charList is a std::list of type char, each element (or member or
    whatever terminology we'll use) can contain one character.


    >
    > char* arp = "Test1";


    arp is a point to char. A char* can point to a single character or
    a zero terminated string or if you've rolled your own string handling
    function perhaps something else. In this case it is pointing to a region
    in memory that contains the characters 'T', 'e', 's', 't', '1', '\0'
    sequentially.

    >
    > if (something)
    > charList.push_back(*arp);



    Assuming that something is true, charList isn't empty anymore and has an
    element that contains the value 'T'. This will become clearer if you
    will...
    >
    > // Display the list


    Here's a little program to do that. I didn't copy and paste, so be careful.

    #include <iostream>
    #include <list>
    int main() {
    std::list<char> cl;
    const char *p1 = "Test";
    const char *p2 = "Help";
    cl.push_back(*p1); // 'T'
    cl.push_back(*p2); // 'H'
    for(std::list<char>::const_iterator i=cl.begin(); i!=cl.end(); i++) {
    std::cout << *i << std::endl;
    }
    }

    Ought to output:
    T
    H

    and not
    Test
    Help




    > list<char>::iterator theIterator;
    > for( theIterator = charList.begin(); theIterator != charList.end();
    > theIterator++ ) {


    I found this, http://www.xsquawkbox.net/xpsdk/mediawiki/XPLMDrawString

    So yes, it looks like you want to pass a char* as the fourth parameter.

    > XPLMDrawString(white, Panel1WindowLeft + 20, Panel1WindowTop - 20,
    > theIterator, 0, xplmFont_Proportional);
    > }




    Consider,

    #include <iostream>
    #include <list>


    void someFakeFunction(char *p) {
    std::cout << p << std::endl;
    }

    int main() {
    std::list<char*> cl;
    char *p1 = "Test";
    char *p2 = "Help";
    cl.push_back(p1);
    cl.push_back(p2);

    for(std::list<char*>::iterator i=cl.begin(); i!=cl.end(); i++) {
    someFakeFunction(*i);
    }
    }

    Ought to output
    Test
    Help

    I don't particularly care for this for several reasons. I think you
    ought to consider using std::string. You might have to write a wrapper
    or some sort of smart pointer to get a non-const pointer to char.

    Since you indicated earlier in the thread that each of these "Test?"
    strings has an associated flag, you may wish to look into std::pair.
    You could make a list like std::list< std::pair<bool, std::string> >.
    Although it's not clear to me why you're using a std::list and not a
    std::vector. Or maybe even just an array.

    Also, your use of magic numbers,
    > XPLMDrawString(white, Panel1WindowLeft + 20, Panel1WindowTop - 20,

    seems a bad thing to me. In the case of your loop above, it looks like
    you will draw over the same space. Your original code changed the
    offsets for each item, also using magic numbers, but might have left
    spaces if a particular text wasn't going to be drawn.


    >
    > I get the error:
    > cannot convert ‘std::_List_iterator<char>’ to ‘char*’ for argument ‘4’
    > to ‘void XPLMDrawString(float*, int, int,
    > char*, int*, XPLMFontID)’
    > Many thanks for help



    I think you can get the compilation error to go away if you change
    theIterator
    to
    &*theIterator
    but you will very likely have problems at run time since I suspect the
    function you're calling expects a zero terminated string and your list
    is of type char.

    LR
     
    LR, Jan 2, 2011
    #5
  6. Michael

    Michael Guest

    list<char> charList;
    char *arp = "TEST0";
    charList.push_back(*arp);

    // Display the list
    list<char>::iterator theIterator;
    for( theIterator = charList.begin(); theIterator != charList.end();
    theIterator++ ) {
    XPLMDrawString(white, Panel1WindowLeft + 20, Panel1WindowTop - 20,
    &*theIterator, 0, xplmFont_Proportional);
    }

    Many thanks. The above works but draws only T. Any idea on how to draw
    all "TEST0"?

    Funny, never seen: &*theIterator
    before in my short C++ life...
     
    Michael, Jan 3, 2011
    #6
  7. Michael

    LR Guest

    Michael wrote:
    > list<char> charList;
    > char *arp = "TEST0";
    > charList.push_back(*arp);
    >
    > // Display the list
    > list<char>::iterator theIterator;
    > for( theIterator = charList.begin(); theIterator != charList.end();
    > theIterator++ ) {
    > XPLMDrawString(white, Panel1WindowLeft + 20, Panel1WindowTop - 20,
    > &*theIterator, 0, xplmFont_Proportional);
    > }
    >
    > Many thanks. The above works but draws only T. Any idea on how to draw
    > all "TEST0"?


    You're not storing all of "Test0" in your list. Only the 'T' since
    you're using a std::list<char>. I think you got lucky that you only
    showed the 'T' and not something like "Tl939wio923i1-0(**(&&*^".

    Please review the post that you're replying to and take a look at the
    example with std::list<char*> I don't like it because I think it will
    cause more problems than it will cure.

    Consider making a small string class of your own, put it in a namespce
    that makes sense for you, give it a method that returns a non-const char
    *. Make your list of this class.


    > Funny, never seen: &*theIterator
    > before in my short C++ life...


    Take the address of the instance of char that theIterator 'points' to.

    I'm still not clear on why you're using a list.

    You might be better off trying this without the call to XPLMDrawString.
    At first write a program that just writes the data to std::cout to see
    what you get.


    LR
     
    LR, Jan 3, 2011
    #7
  8. Michael

    Michael Guest

    Now if i try:


    do{
    charList.push_back(*arp);
    }while(*arp);


    I get an endless loop. How should i do that?
    Many thanks again
    Michael
     
    Michael, Jan 4, 2011
    #8
  9. Michael

    LR Guest

    Michael wrote:
    > Now if i try:
    >
    >
    > do{
    > charList.push_back(*arp);
    > }while(*arp);
    >
    >
    > I get an endless loop. How should i do that?


    I don't know why you would want to do this.

    Consider making an array of pointers to char,
    char *a[]
    and using a for loop to index through the array.

    Try initializing the array and printing out all the values.


    Are you using a book?

    What compiler are you using?

    LR
     
    LR, Jan 4, 2011
    #9
  10. Michael

    Michael Guest

    If I try something alike:

    list<char> charList;
    char displayTxt[50];
    displayTxt[0]= "Test1";

    for (int i =1; i <= 50; i++)
    charList.push_back(displayTxt);

    I get: invalid conversion from ‘const char*’ to ‘char




    > I don't know why you would want to do this.
    >

    I want a display. New values are added at the bottom. Untrue values are
    not displayed anymore and all values below are moved one position up:

    Panel1WindowTop - 20 // draw position
    etc.


    > Consider making an array of pointers to char,
    > char *a[]
    > and using a for loop to index through the array.
    >
    > Try initializing the array and printing out all the values.
    >
    >
    > Are you using a book?
    >

    Jesse liberty c++ in 21 days.
    > What compiler are you using?

    GCC Linux and Mac later also VC.
    Thanks again
    Michael
     
    Michael, Jan 6, 2011
    #10
  11. Michael

    LR Guest

    Michael wrote:
    > If I try something alike:
    >
    > list<char> charList;
    > char displayTxt[50];




    > displayTxt[0]= "Test1";


    As was pointed out by Daniel T elsethread the above is the cause of the
    error message you're getting below.

    displayTxt[0] = 'T';
    displayTxt[1] = 'e';
    displayTxt[2] = 's';
    displayTxt[3] = 't';
    displayTxt[4] = '1';
    displayTxt[5] = '\0';

    would do what I think you would like your assignment statement to do.

    A char is only a single character. It doesn't contain a string of
    characters. Very loosely speaking, "Test1" is like an array of char
    somewhere in memory. That is, a string is sort of like an array of char.

    char x[10]; doesn't mean an array of ten strings, but an array of ten char.

    Consider trying this snippet,

    char s[] = "hello";
    std::cout << s << std::endl;

    s[2] = 't';
    std::cout << s << std::endl;

    >
    > for (int i =1; i <= 50; i++)
    > charList.push_back(displayTxt);


    You'll end up with a list of char with one character in each member of
    the list.

    >
    >> I don't know why you would want to do this.
    >>

    > I want a display. New values are added at the bottom. Untrue values are
    > not displayed anymore and all values below are moved one position up:
    >
    > Panel1WindowTop - 20 // draw position
    > etc.


    At this point you seem to be doing more C style programming than C++,
    even though you want to use std::list.

    At some point you'll find the C++ faq useful, but at the moment you
    might find more immediate help in the C faq.

    http://c-faq.com/
    http://c-faq.com/ptrs/index.html
    http://c-faq.com/aryptr/index.html

    The C++ faq is here
    http://www.parashift.com/c -faq-lite/

    Also, IIRC Alf P. Steinbach who posts here frequently used to have a
    pretty good tutorial posted somewhere online about C++ pointers. I
    can't find it at the moment. Perhaps you should ask him if he'd be so
    kind as to provide a link to it or resurrect it if it's not online at
    the moment.

    Please read up on std::string, it might save you some trouble.




    >> Consider making an array of pointers to char,
    >> char *a[]
    >> and using a for loop to index through the array.
    >>
    >> Try initializing the array and printing out all the values.


    This may still be worth trying as an exercise.

    LR
     
    LR, Jan 7, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    756
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    493
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    693
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    397
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,302
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page