Hi,

i have the following class :

class Book
{
string Category;
string SubCategory;

Book(string Cat, string SubCat)
{
Category = Cat;
SubCategory = SubCat;
};
};

in my cpp file, i use a vector of this class:

vector<Book> My_Book;
My_Book.push_back(Book("Cat1","SubCat1"));
My_Book.push_back(Book("Cat2","SubCat2"));
My_Book.push_back(Book("Cat3","SubCat3"));

// this works great...but how can i display later my data ?
i tried :

char buf[1024];
for(int i=0;i<My_Book.size();i++)
{
wsprintf(buf,"Element %d : %s\n",i+1,&My_Book.at(i).Category);
TRACE(buf);
}

but it doesn't work... i mean &My_Book.at(i).Category
i have partially good result...but just partially :(
something like : IIIICat1 and IIIIcat2,....

thx for help,
Maileen

Re: vector and class - end by Bo

Bo
Sat Jun 19 16:34:38 CDT 2004


"Maileen" <nospan@email.com> skrev i meddelandet
news:OC%23W1piVEHA.2944@tk2msftngp13.phx.gbl...
> Hi,
>
> i have the following class :
>
> class Book
> {
> string Category;
> string SubCategory;
>
> Book(string Cat, string SubCat)
> {
> Category = Cat;
> SubCategory = SubCat;
> };
> };
>
> in my cpp file, i use a vector of this class:
>
> vector<Book> My_Book;
> My_Book.push_back(Book("Cat1","SubCat1"));
> My_Book.push_back(Book("Cat2","SubCat2"));
> My_Book.push_back(Book("Cat3","SubCat3"));
>
> // this works great...but how can i display later my data ?
> i tried :
>
> char buf[1024];
> for(int i=0;i<My_Book.size();i++)
> {
> wsprintf(buf,"Element %d : %s\n",i+1,&My_Book.at(i).Category);
> TRACE(buf);
> }

wsprintf is an old C function that doesn't have formats for composite
types. You could try some C++ I/O instead:

std::cout << "Element " << i << " : " << My_Book[i].Category << '\n';


Bo Persson¨



Re: vector and class - end by Sergei

Sergei
Mon Jun 21 02:46:25 CDT 2004

"Maileen" <nospan@email.com> wrote in message news:OC%23W1piVEHA.2944@tk2msftngp13.phx.gbl...
> Hi,
>
> i have the following class :
>
> class Book
> {
> string Category;
> string SubCategory;
>
> Book(string Cat, string SubCat)
> {
> Category = Cat;
> SubCategory = SubCat;
> };
> };
>
> in my cpp file, i use a vector of this class:
>
> vector<Book> My_Book;
> My_Book.push_back(Book("Cat1","SubCat1"));
> My_Book.push_back(Book("Cat2","SubCat2"));
> My_Book.push_back(Book("Cat3","SubCat3"));
>
> // this works great...but how can i display later my data ?
> i tried :
>
> char buf[1024];
> for(int i=0;i<My_Book.size();i++)
> {
> wsprintf(buf,"Element %d : %s\n",i+1,&My_Book.at(i).Category);

What if you try this:

wsprintf(buf,"Element %d : %s\n",i+1, My_Book.at(i).Category.c_str());

?
Sergei

> TRACE(buf);
> }
>
> but it doesn't work... i mean &My_Book.at(i).Category
> i have partially good result...but just partially :(
> something like : IIIICat1 and IIIIcat2,....
>
> thx for help,
> Maileen

Re: vector and class - end by ak

ak
Sat Jun 26 06:26:19 CDT 2004

On Sat, 19 Jun 2004 20:22:59 +0200, Maileen <nospan@email.com> wrote:

>>Hi,
>>
>>i have the following class :
>>
>>class Book
>>{
>> string Category;
>> string SubCategory;
>>
>> Book(string Cat, string SubCat)
>> {
>> Category = Cat;
>> SubCategory = SubCat;
>> };
>>};
>>

try

class Book
{
public:
string Category;
string SubCategory;

Book(string Cat, string SubCat)
{
Category = Cat;
SubCategory = SubCat;
}
};


>>in my cpp file, i use a vector of this class:
>>
>>vector<Book> My_Book;
>>My_Book.push_back(Book("Cat1","SubCat1"));
>>My_Book.push_back(Book("Cat2","SubCat2"));
>>My_Book.push_back(Book("Cat3","SubCat3"));
>>
>>// this works great...but how can i display later my data ?
>>i tried :
>>
>>char buf[1024];
>>for(int i=0;i<My_Book.size();i++)
>>{
>> wsprintf(buf,"Element %d : %s\n",i+1,&My_Book.at(i).Category);
>> TRACE(buf);
>>}

your member variables are strings which encapsulates the normal char ptr, in
order to access the ptr you need to use the c_str() member function:

TRACE( "%s\n", m_Book.at(i).Category.c_str() );

wsprintf is normally used for wide characters(wchar_t) so if you
want to store widechars in your class you should use wstring and then
use the same member function to print out: c_str()

wchar_t buf[1024];
wsprintf( buf, L""Element %d : %ls\n" ,i+1, My_Book.at(i).Category.c_str());


hth
ak