undefined reference to a static variable

Hi Everybody,

I have 3 classes, A, B and C.
1
2
3
4
5
6
7
8
9
10
11
12
class A {
	private:
	int   N;	

	public:
	int  *VectorInA;		// Size N

	void  SetN_A(int n){ N = n; }   // This is used by the constructor of
                                        // the derived class described above. 

	(... everything else ...)
};

The other classes look more or less the same.

After, I construct 3 derived classes AB, AC and BC. These contain no data, but functions relating the Vectors In A B and C.
1
2
3
4
5
class AB: public A, public B{

      (... stuff ...)

};

The size of the vectors N, is the same for all classes.

I have 3 questions.

1:
If I write static int N instead of int N, I get the error message
undefined reference to N
the same number of times I try to use this static variable.
Why?

(I don't mind just int, but I want to know why.)

2:
If I don't include a variable N in the classes AB etc, the compiler says that there are ambiguities when this variable is called. I really don't want to name each one NA NB and NC in the classes A B and C but there has to be a better way to deal with this problem rather than introducing yet more variables N (one in each class AB, AC and BC) whose value will be the same.

3:
In the constructors of the classes AB AC and BC, I write:
 
VectorInA = new int[N]

If I don't, I get errors
free(): invalid pointer:
etc. However, if I write
 
delete [] VectorInA; delete [] VectorInB;

in the destructor of the classes AB etc. I get
double free or corruption (fasttop)
etc.

This makes me thing that the destructor of the classes A and B are called in the destructor of the derived classes, but NOT the constructor. (However, my bibliography states the opposite.)
I just want to make sure that I don't have a memory leak. Suggestions?

By the way, I use g++.

Thanks !!
Last edited on
Which destructor are you talking about? Class A's destructor should delete only VectorInA and Class B's destructor should only delete VectorInB. Of course if you actually used the std::vector in the first place it is a non-issue.

As far as the multiple inheritance issue goes have you tried the scope resolution operator? You might need to specify which N you wish to use at any time. A::N, B::N, C::N, and so forth. Also see sections 25.8 and 25.9 below. I don't see your entire example so it doesn't sound like you are dealing with the dreaded diamond however some of the info in these FAQ's might help you anyway.
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8

Making those variables static isn't the answer at all so I'm not going to bother going into detail on that. Just don't do it because it won't solve your problem.
Which destructor are you talking about? Class A's destructor should delete only VectorInA and Class B's destructor should only delete VectorInB.


I thought I was clear, but here I go again:

In the constructors of the derived class, AB I allocate the memory for the vectors in A and B, otherwise I get an error. In the destructor of the derived class AB, if I delete the vectors, I get
double free or corruption (fasttop)
.

This makes me think that the constructor of the class A and the class B is NOT called when the constructor of the derived class AB is called. However, the destructor of the classes A and B is called when the destructor of the class AB is called. My bibliography stated the opposite. What is actually going on?

I know that static int will not solve my problem. (Trial and error.)
But, does anybody knows why? or what does the error means?

kempofighter, thanks for the reference.

Cheers,
Last edited on
Well it isn't clear because you are trying to describe it instead of just posting the code. That always makes things difficult. static variables are not a part of the object. They are special. You probably forgot to implement them in the .cpp files which is a common mistake.

You really shouldn't be setting be setting the pointers in the most derived class in the first place. I wouldn't know why you are getting those errors unless you post an example. Post the code for class A, B, and AB and make a main function that constructs an AB so that we can see what you are doing and compile it for ourselves. It doesn't have to be fancy - just enough for someone else to duplicate the compiler error.
Topic archived. No new replies allowed.