LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2005, 11:14 PM   #1
crapodino
Member
 
Registered: Aug 2005
Distribution: SuSE 10
Posts: 72

Rep: Reputation: 15
problem with c++ in Suse Prof. 9.3


Hi! i have suse prof 9.3 and i use kdevelope. i have a definition of a class in a .hpp like this:

//prueba.hpp
#include <stdio.h>
#include <iostream>

using namespace std;
class TH_Atender_Cliente //: public Thread
{

public:

static int i;

TH_Atender_Cliente() { i=1;/*pthread_mutex_init(&sem,NULL); */}
~TH_Atender_Cliente() {}
};


and then i have:

//hola.cpp

#include <iostream>
#include "prueba.hpp"
using namespace std;

int main() {

TH_Atender_Cliente unThread;

return 0;
}


Now, the problem is that the kdevelop compile perfecty, even without a single warning... but when i want to compile it with g++, por exsample like this: g++ hola.cpp -o hola
it appear a error like this:

hola.cpp: undefined reference to `TH_Atender_Cliente::i'
collect2: ld returned 1 exit status

In fact, my problem is that i can't use the staticl attribues. For instance, if i have a method than need to use a static attribute, y can't because i have that same error.

Before i did that i had a .cpp where i put the definition of the methods, but thes as i couldn't acces the static attributes, i tried to put all togheter in a .cpp, but the same...

In the example i tried to acces a static attribute in the constructor

please if anyone nows!!!

i think it must be somthing of the g++ because with kdevelope y compile pefectly...

thaks
 
Old 09-25-2005, 12:45 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
You need to add the line
Code:
int TH_Atender_Cliente::i;
after the declaration of the TH_Atender_Cliente class to define "i".
 
Old 09-25-2005, 09:19 AM   #3
crapodino
Member
 
Registered: Aug 2005
Distribution: SuSE 10
Posts: 72

Original Poster
Rep: Reputation: 15
ok, but....

Quote:
Originally posted by spooon
You need to add the line
Code:
int TH_Atender_Cliente::i;
after the declaration of the TH_Atender_Cliente class to define "i".
thanks, i did that and it works, but my quistion is: i always have to do that? with all the statics attributes i have? each of them?? i really don't undestood why i have to declear two times the same attribute...why is this??

yes, i know i am a bit newie but i am staring with this lenguaje....

thanks
 
Old 09-25-2005, 09:54 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You declare i here:
Code:
 class TH_Atender_Cliente //: public Thread
{

public:

static int i;
...
You define i here:
Code:
int TH_Atender_Cliente::i;
Suppose you didn't create any instances of "TH_Atender_Cliente". The compiler is expecting "i" (one "i") to be defined somewhere. Where, if you don't tell it?

Now suppose you declare a single instance of "TH_Atender_Cliente ". Or better, suppose you declare multiple instances of "TH_Atender_Cliente". Which one defines the "one, true i"? The answer, of course, is none of them.

You always have to define a global variable some one place (and use "extern" to reference it from every other place). The situation is no different for a static class member (like "TH_Atender_Cliente::i).

Last edited by paulsm4; 09-25-2005 at 09:56 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Suse 9.1 prof, special characters problem klodoma Linux - Newbie 1 09-25-2005 10:01 AM
SUSE Prof 9-1 berri Linux - Newbie 2 07-27-2005 12:39 AM
Problem with SUSE 9.2 Prof/Windows XP Prof Dual Booting sm000ve Linux - Newbie 1 11-29-2004 07:21 PM
suse 9.1 prof to suse 9.2 prof update? ronss SUSE / openSUSE 4 11-04-2004 07:54 AM
Shutdown problem Suse 9.0 Prof KDE finnhs Linux - Distributions 1 02-15-2004 03:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration