468,510 Members | 1,745 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

C++ string global variable

I am pretty new to using C++ and I can't figure this out.

I have class PTM that starts a thread. There is an array of structure
to be shared between PTM class and thread function. For some reason,
string data gets lost when it gets to the thread function.

----------
in PTM.h
----------
using namespace std;

struct process_list{
string title;
int status;
int num_restart;
string comments;
};
-----------
in PTM.cpp
-----------
process_list global_process[10];
int process_num ;

PTM::PTM(){
loadConfig()
CreateThread(...,LPTHREAD_START_ROUTINE)watchdog,. ..);

}

PTM::loadConfig() {
for (int i =0; i < condition; i++) {
global_process[i].title= "gets data";
global_process[i].status = 0;
}
process_num = i;
}

//global function in the file scope watchdog
int watchdog() {
//print the global variables
for (int j =0 ; j < process_num; j++) {
printLog("%s has died", global_process[i].title;
}

}

I ran it thru debugger. It seems like watchdog() sees process_num(int
type) global variable. It also sees status, num_restart in
globa_process but not title or comment which are string type.
loadConfig seems to work fine too.

Any ideas, comments, help will be greatly appreciate.

Thanks in advance,

Seung

Jul 23 '05 #1
2 2639
On 9 Feb 2005 23:16:09 -0800 in comp.lang.c++, "semi"
<mi*****@yahoo.com> wrote,
I am pretty new to using C++ and I can't figure this out.

I have class PTM that starts a thread. There is an array of structure
to be shared between PTM class and thread function. For some reason,
string data gets lost when it gets to the thread function.

----------
in PTM.h
----------
using namespace std;
Bad form, forcing a "using namespace" on all the possible includers of
your header.

....
struct process_list{
string title;
for (int j =0 ; j < process_num; j++) {
printLog("%s has died", global_process[i].title;


I don't see your definition of printLog, but I venture to guess that
it knows nothing of std::string. Perhaps it would be happier with
global_process[i].title.c_str()

How did you compile with unmatching parenthesis?

Jul 23 '05 #2
Obviously it is not the exact code I have.
It was a pseudo coded to be posted. My printLog does what c_str()
does.

Thanks for pointing out the namespace use in .h
David Harmon wrote:
On 9 Feb 2005 23:16:09 -0800 in comp.lang.c++, "semi"
<mi*****@yahoo.com> wrote,
I am pretty new to using C++ and I can't figure this out.

I have class PTM that starts a thread. There is an array of structureto be shared between PTM class and thread function. For some reason,string data gets lost when it gets to the thread function.

----------
in PTM.h
----------
using namespace std;
Bad form, forcing a "using namespace" on all the possible includers

of your header.

...
struct process_list{
string title;
for (int j =0 ; j < process_num; j++) {
printLog("%s has died", global_process[i].title;


I don't see your definition of printLog, but I venture to guess that
it knows nothing of std::string. Perhaps it would be happier with
global_process[i].title.c_str()

How did you compile with unmatching parenthesis?


Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Phrederik | last post: by
4 posts views Thread by Jonathan Burd | last post: by
2 posts views Thread by Elizabeth Graham | last post: by
4 posts views Thread by A.M | last post: by
1 post views Thread by andy_w_irvine | last post: by
18 posts views Thread by greenflame | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.