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
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!
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*.
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) __________
> wxPrintf(_T("%s\n"), g.table); // here doesn't work - i've got empty
try g.table.c_str()
HTH
Rudi
> 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";
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
> 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.
U�ytkownik "Daniel T." <dani...@earthlink.net> napisa� w wiadomo�ci
news:daniel_t-2CC3A4...@earthlink.vsrv-sjc.supernews.net...
U�ytkownik "Daniel T." <dani...@earthlink.net> napisa� w wiadomo�ci
news:daniel_t-C29766...@earthlink.vsrv-sjc.supernews.net...
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 .