object instance

hi,

please consider this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
==============
// file1.h
#ifndef C_1
#define C_1

class cl1{

 cl1();
 ~cl1();
// member functions
 mem_foo();
};

void  foo();
#endif;
// file1.cpp

#include "file1.h"

C_1 Test;

void  foo(){

Test.mem_foo();

}

cl1::cl1(){.....}

cl1::~cl1(){}

=============
//file2.h
#ifndef C_2
#define C_2

#include file1.h

class cl2{

 cl2();
 ~cl2();
// member functions
mem_foo();

};
#endif;

// file2.cpp
.....

C_2::mem_foo(){

foo();  // call the function from the file1.h

}
==================


by linking the program the compiles throws one error: multiple definition of "Test"

1- what i have done wrong?

2- can i initialize the instance of the class C_1 in the same header file just like "Test" and by its definition accesses the member functions?

thank for each suggestion
Last edited on
You have it in the header file, so each .cpp might get an instance of "Test".

Maybe, but you shouldn't. It's like creating a template function that is supposed to sort something but uses a random global variable.
@firedraco, thanks. but could you clerify more what you said in second line. how can i can i initialize it as a random global variable?

thanks
@firedraco, i have declared and initialized the object in the source file. i did not get the error more by linking. but when i execute the program it will be broken directly. it throws "segmentation error". So how can i declare the object "Test" in the best way?

thank
I would declare it as a global object (assuming you really want it to be global). With that, you won't be able to have a member function reference that though. I have a question though, why do you need a member function that needs a random global variable? Could you pass it as a parameter or something instead?
@firedraco. you are right. but sometimes you get some application from someone else and you are not allow to modify for example some interface function. therefore you may need what i need now :)

So, how can i implement this object as a global object? and what you mean with "you won't be able to have a member function reference that though."??

I'm not sure I understand what you are trying to do then...what exactly is this member function supposed to be doing? Member functions shouldn't need global variables to exist, they should be dependent on the whatever object they are modifying (unless they are static, but they still shouldn't need global variables).
@firedraco. I do not know which member functions do you mean. But let us consider the above case again after ihave modified the code

I want to call the foo() function from file2.cpp. This foo() function is and implemented in the file1.cpp. but it is not a member function of the class cl1.

The problem is now by instantiating the object of class cl1. when i do that i get the execution will be broken directly.

Now may someone has any suggestion please to tell me how can i implement this object according to the above case correctly.

thanks
any suggestion please!!!
Topic archived. No new replies allowed.