By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

C++ string global variable

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.