c ++ - 2-dimensional array af TCHAR

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at oprette 2 matricer: 1 af char * og 1 af THAR *. Men for TCHAR * matrix i stedet for strenge får jeg adresser af en slags. Hvad er der galt?


Kode:


#include <tchar.h>
#include <iostream>

using namespace std;

int main(int argc, \_TCHAR* argv[])
{
    //char
    const char* items1[2][2] = {
        {"one", "two"},
        {"three", "four"},
    };

    for (size\_t i = 0; i < 2; ++i)
    {
        cout << items1[i][0] << "," << items1[i][1] <<endl;
    }

    /*
    Correct output:
        one,two
        three,four
    */

    //TCHAR attempt
    const TCHAR* items2[2][2] = {
        {\_T("one"), \_T("two")},
        {\_T("three"), \_T("four")},
    };

    for (size\_t i = 0; i < 2; ++i)
    {
        cout << items2[i][0] << "," << items2[i][1] <<endl;
    }

    /*
    Incorrect output:
        0046AB14,0046AB1C
        0046AB50,0046D8B0
    */

    return 0;
}

Bedste reference


For at løse problemet skal vi bruge wcout til Unicode-strenge. Brug Sådan cout the std :: basic\_string < TCHAR > vi kan skabe fleksible tcout:


#include <tchar.h>
#include <iostream>

using namespace std;

#ifdef UNICODE
    wostream& tcout = wcout;
#else
    ostream& tcout = cout;
#endif // UNICODE

int main(int argc, \_TCHAR* argv[])
{
    //char
    const char* items1[2][2] = {
        {"one", "two"},
        {"three", "four"},
    };

    for (size\_t i = 0; i < 2; ++i)
    {
        tcout << items1[i][0] << "," << items1[i][1] <<endl;
    }

    /*
    Correct output:
        one,two
        three,four
    */

    //TCHAR attempt
    const TCHAR* items2[2][2] = {
        {\_T("one"), \_T("two")},
        {\_T("three"), \_T("four")},
    };

    for (size\_t i = 0; i < 2; ++i)
    {
        tcout << items2[i][0] << "," << items2[i][1] <<endl;
    }

    /*
    Correct output:
        one,two
        three,four
    */

    return 0;
}