Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

STL simple problem

1 view
Skip to first unread message

wizard

unread,
Jun 30, 2009, 8:05:23 AM6/30/09
to
Why it doesn't work

class Tnames
{
public:
wxString text;
wxString table;
};

std::vector<Tnames> wektor;

for (size_t i = 0; i < count; ++i)
{
Tnames temp;

temp.text="text";

wektor.push_back(temp);
}


for (int i=0; i<p_wek_t.size(); i++)
{
Tnames g = p_wek_t.at(i);

g.table = "table";
}

for (i=0; i<p_wek_t.size(); i++)
{
Tnames g = p_wek_t.at(i);

wxPrintf(_T("%s\n"), g.table); // here doesn't work - i've got empty
string here
}

I'm beginner on STL ;so Could somebody help me
Greg


__________ Informacja programu ESET NOD32 Antivirus, wersja bazy sygnatur wirus�w 4075 (20090514) __________

Wiadomosc zostala sprawdzona przez program ESET NOD32 Antivirus.

http://www.eset.pl lub http://www.eset.com


Alf P. Steinbach

unread,
Jun 30, 2009, 8:07:28 AM6/30/09
to
* wizard:

> Why it doesn't work
>
> class Tnames
> {
> public:
> wxString text;
> wxString table;
> };
>
> std::vector<Tnames> wektor;
>
> for (size_t i = 0; i < count; ++i)
> {
> Tnames temp;
>
> temp.text="text";
>
> wektor.push_back(temp);
> }
>
>
> for (int i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
>
> g.table = "table";
> }
>
> for (i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
>
> wxPrintf(_T("%s\n"), g.table); // here doesn't work - i've got empty
> string here
> }
>
> I'm beginner on STL ;so Could somebody help me

This looks like some wxWidgets code.

It has nothing do with STL (except the use of std::vector).

Probably wxString doesn have an implicit conversion to char const*, and probably
wxPrintf, with format specification %s, requires a char const*.

For more informed answer try to

* Ask in a group where the library you're using is on-topic.

* Show actual code, not made-up code.

* Copy and paste the code, don't re-type it.

Cheers & hth.,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!

Alf P. Steinbach

unread,
Jun 30, 2009, 8:09:00 AM6/30/09
to
* Alf P. Steinbach:

> * wizard:
>> Why it doesn't work
>>
>> class Tnames
>> {
>> public:
>> wxString text;
>> wxString table;
>> };
>>
>> std::vector<Tnames> wektor;
>>
>> for (size_t i = 0; i < count; ++i)
>> {
>> Tnames temp;
>>
>> temp.text="text";
>>
>> wektor.push_back(temp);
>> }
>>
>>
>> for (int i=0; i<p_wek_t.size(); i++)
>> {
>> Tnames g = p_wek_t.at(i);
>>
>> g.table = "table";
>> }
>>
>> for (i=0; i<p_wek_t.size(); i++)
>> {
>> Tnames g = p_wek_t.at(i);
>>
>> wxPrintf(_T("%s\n"), g.table); // here doesn't work - i've got
>> empty string here
>> }
>>
>> I'm beginner on STL ;so Could somebody help me
>
> This looks like some wxWidgets code.
>
> It has nothing do with STL (except the use of std::vector).
>
> Probably wxString doesn have an implicit conversion to char const*, and
> probably wxPrintf, with format specification %s, requires a char const*.

Sorry, probably wxPrintf is a variadic function (argument list "..."), in which
case implicit conversion or not doesn't matter, but what matters is that you
pass the type specified by %s, which probably is char const*.

wizard

unread,
Jun 30, 2009, 8:27:34 AM6/30/09
to
I think my problem is not applying printf func.
When I printf text variable using wxPrintf I have no problem
The problem is when I put my first var (text) and allocate memory using
push_back
and after that putting second var (table)

class Tnames
{
public:
wxString text;
wxString table;
};

std::vector<Tnames> wektor;

for (size_t i = 0; i < count; ++i)
{
Tnames temp;

temp.text="text";

wektor.push_back(temp);
}

__________ Informacja programu ESET NOD32 Antivirus, wersja bazy sygnatur wirus�w 4075 (20090514) __________

Rüdiger Ranft

unread,
Jun 30, 2009, 8:44:26 AM6/30/09
to
wizard schrieb:

> wxPrintf(_T("%s\n"), g.table); // here doesn't work - i've got empty

try g.table.c_str()

HTH
Rudi

Daniel T.

unread,
Jun 30, 2009, 8:48:32 AM6/30/09
to
"wizard" <gg...@wp.pl> wrote:

> for (int i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
> g.table = "table";
> }
>

> // error: 'i' was not declared in this scope


> for (int i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
>

> wxPrintf(_T("%s\n"), g.table); // here doesn't work
> }

The above is correct behavior. In the first loop you make a copy of
p_wek_t.at(i) and then set the *copy's* table value to "table", that
doesn't change the object that is actually contained in p_wek_t.

Try this instead:

for (int i = 0; i < p_wek_t.size(); ++i)
p_wek_t.at(i).table = "table";

Thomas J. Gritzan

unread,
Jun 30, 2009, 9:05:55 AM6/30/09
to
wizard schrieb:

> Why it doesn't work
>
> for (int i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
>
> g.table = "table";

You copy the object in the vector to a new object called 'g' and modify
the copy. At the end of the loop body, the copy is destroyed.

The original object in the vector is unmodified.

> }
>
> for (i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
>
> wxPrintf(_T("%s\n"), g.table); // here doesn't work - i've got empty
> string here

Here, you make another copy of the same unmodified objects in the vector.

> }

To be able to modify objects in the vector, you have to bind a
(non-const) reference to the original objects in the vector:

Tnames & g = p_wek_t.at(i);

--
Thomas

Daniel T.

unread,
Jun 30, 2009, 9:13:35 AM6/30/09
to
"wizard" <gg...@wp.pl> wrote:

> Why it doesn't work
>
> class Tnames
> {
> public:
> wxString text;
> wxString table;
> };
>
> std::vector<Tnames> wektor;
>
> for (size_t i = 0; i < count; ++i)
> {
> Tnames temp;
>
> temp.text="text";
>
> wektor.push_back(temp);
> }
>
>
> for (int i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
>
> g.table = "table";
> }
>
> for (i=0; i<p_wek_t.size(); i++)
> {
> Tnames g = p_wek_t.at(i);
>
> wxPrintf(_T("%s\n"), g.table); // here doesn't work - i've got empty
> string here
> }
>
> I'm beginner on STL ;so Could somebody help me

I try not to be too pedantic about posting compilable code examples and
all that, however there are numerous errors and style issues in the code
you posted that made it very difficult to find the problem you were
specifically asking about.

1) Unnecessary use of non standard constructs: This group deals with the
language itself and not in particular libraries. Using standard language
in your examples here and at least explaining how non-standard classes
work helps tremendously.

2) Gratuitous use of undefined variables: Several of the variables in
your example are undefined, including the single most important one,
'p_wek_t'.

3) Gratuitous inclusion of code that doesn't affect the problem: The
first loop in your example has absolutely nothing to do with the problem
you needed help on.

4) Poor formatting: Your indents are inconsistent.

Alf has an excellent understanding of C++, yet he didn't spot your
problem. I suspect that the reason is due to one or more of the issues I
outline above.

The only reason I spotted your problem is because I took the time to put
your code in my compiler and fix all the errors and get it to compile.
Do as much of the work as you can yourself, and you are more likely to
get quick and useful help from this group.

wizard

unread,
Jun 30, 2009, 9:20:21 AM6/30/09
to
Thanks Daniel for Your fast help
Now it works excellent
Greg

U�ytkownik "Daniel T." <dani...@earthlink.net> napisa� w wiadomo�ci
news:daniel_t-2CC3A4...@earthlink.vsrv-sjc.supernews.net...

wizard

unread,
Jul 1, 2009, 5:07:02 AM7/1/09
to
Sorry for my previous mistatekes
I'm new on this group so I have made lots of mistakes.
I will try to explain more closely my problem on future.
Sorry You had to waste a lot of time to solve my simple problem.
But till now I was on C programming and now trying to port on STL C++.
I see the adventage but sometimes I have a big problem for me and have noone
who could help me. So looking forward to help from outside (professional
coders I mean)
Regards
Greg

U�ytkownik "Daniel T." <dani...@earthlink.net> napisa� w wiadomo�ci

news:daniel_t-C29766...@earthlink.vsrv-sjc.supernews.net...

Jorgen Grahn

unread,
Jul 2, 2009, 9:10:17 AM7/2/09
to
On Tue, 30 Jun 2009 14:27:34 +0200, wizard <gg...@wp.pl> wrote:
> I think my problem is not applying printf func.
> When I printf text variable using wxPrintf I have no problem
> The problem is when I put my first var (text) and allocate memory using
> push_back
> and after that putting second var (table)

I don't understand that. Someone (not Alf, someone else) already
pointed out your primary bug: taking a copy of an element in the
vector, modifying the copy, and then expecting the original to have
magically changed, too. (As if it was Java code, where pretty much
every name is a reference).

(Or maybe you are talking about a second problem now.)

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

0 new messages