Inherit class form different header file

Hi,
I have a problem with my project.
I need to inherit a class vector from element class , heres what i have now , i tried alot of things but they didnt work:
This is vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef VECTOR_H  
#define VECTOR_H  
//#include "element.h"
#include <iostream>
#include <math.h>
using namespace std;

template <class T>
class vector //: public element
{
//code

};
#endif 

this is my element.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef ELEMENT_H extern 
#define ELEMENT_H extern 
#include "vector.h"
class element{
public:
	template <clas U>
	template <class T>
	//virtual T plus(const U& vec);
	template <clas U>
	template <class T>
	//virtual	T minus(const U& vec);
};
#endif 


This is not compiling not sure what i am doing wrong never did that.
Thanks
Obligatory link: http://cplusplus.com/forum/articles/10627/

It looks like you're having a circular include problem (vector is including element, and element in including vector).

There's no need for that. element probably doesn't need to know about vector.

And you're spelling 'class' wrong in a few spots ('clas').

And you probably don't need to double template like that. You can just have two template params, right?

My recommended changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef VECTOR_H  
#define VECTOR_H  
#include "element.h"  // element is an include dependency.  #include it here
#include <iostream>
#include <cmath>  // changed to cmath
// using namespace std;  // terrible idea, especially since you're naming your
  // class 'vector'.  This conflicts with std::vector in a big way.

template <class T>
class vector : public element  // this can work now
{
//code

};
#endif  


1
2
3
4
5
6
7
8
9
10
11
12
#ifndef ELEMENT_H extern 
#define ELEMENT_H extern 
//#include "vector.h"   vector.h is not an include dependency.   Don't include it.
class element{
public:
	template <class T, class U>  // changed from double template
	virtual T plus(const U& vec);

	template <class T, class U>  // ditto
	virtual	T minus(const U& vec);
};
#endif  
Last edited on
Hi,
Thank you for your reply,
This is giving me more errors now i dont know know why .
heres my full class implementation : my program should inherit math and minus from the super class element ; the thing is one class will use the function to add two listing and the 2nd class will use it to add two class vectors that i created .
element.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef ELEMENT_H extern 
#define ELEMENT_H extern 
#include "vector.h"
class element{
public:
	template <class U>
	template <class T>  // changed from double template
	virtual vector<T> plus(const vector<U>& vec);
	template <class U>
	template <class T>  // ditto
	virtual	vector<T> minus(const vector<U>& vec);
};
#endif 


vector.h
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
#ifndef VECTOR_H  
#define VECTOR_H  
#include "element.h"
#include <iostream>
#include <math.h>
using namespace std;

template <class T>
class vector : public element 
{
public:
	vector(){vector(0,0,0);};
    vector(T ,T ,T );
    friend ostream& operator<< <> (ostream& output,vector<T>& );
	friend istream& operator>> <>(istream& in, vector<T>&);
	//template <class U>
	T operator+(const T &rhs);
	vector<T> operator-(const vector<T> &rhs);
	bool operator==(const vector<T> &rhs);
	vector<T> operator+(const vector<T> &rhs);
	vector<T> operator -();
	template <class U>
    vector<T> plus(const vector<U>& vec);
	vector<T> minus(const vector<T>& vec);
	vector<T> cross(const vector<T>& vec);
	T dot(const vector<T>& vec);
	vector<T> times(const T&);
	T length();
//private:////
    T x,y,z;
};
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
template<class T>
template<class U>
vector<T> vector<T>::plus(const vector<U>& vec)
{
     vector<T> result(x+vec.x,y+vec.y,z+vec.z); 
	 return result;
}
template <class T>
vector<T> vector<T>::minus(const vector<T>& vec)
{
	 vector<T> result(x-vec.x,y-vec.y,y-vec.z); 
     return result;
}

1) I just realized... you have virtual template functions. You can't do that. Template functions can't be virtual.

2) And I still don't know why you're going back to double templates like that. You don't need them here. I guess it doesn't matter, but ew.

3) And you still have the circular include problem. If vector.h is including element.h, then element.h can't include vector.h. See the link I posted earlier: http://cplusplus.com/forum/articles/10627/ Read section 4. If element really needs to have vector as part of it (although I don't see why that'd be the case, but that's another issue), then vector needs to be forward declared.

4) You also have const correctness issues, but that probably isn't causing [m]any errors

5) Since you're naming your class "vector", using namespace std IS A TERRIBLE TERRIBLE IDEA. You're asking for problems with this. vector is the name of an already existing class, and the only thing stopping your vector class from clashing with std::vector is the std namespace. If you use the using directive, you throw that protection out the window and open yourself up to all sorts of problems. Even if you don't include <vector> yourself, other headers might do it without you realizing.

6) Your real problem seems to be a design problem. Why do you need to derive from 'element'? What purpose does element serve here? It seems to me like you need to rethink your whole approach here.
Just to reiterate what has already been said about using namespace std;.

Please DO NOT put that in header files. You're asking for trouble.
Last edited on
Hi,
Thanks again for the reply,,
I see the problems you are mentioning here , to be honest this is a school project i never inherit class from another header file .
As i said the reason for implementing class element is that function plus and minus has to be inherited from element into class vector and another class , but if i figure out one i will be able to do the 2nd one.
For the template function i am not really sure if i am doing it right .
The plus function for example will be used by vector class to do what it does right now adding two vectors and that works fine .
the plus function in another class which is as i said inherited from element will add two lists .
As i said the reason for implementing class element is that function plus and minus has to be inherited from element into class vector and another class , but if i figure out one i will be able to do the 2nd one.


This doesn't really make sense to me. Especially not with now these functions are formed.

What's the other class that will be deriving from element?

Your element function takes and returns vectors, so whatever other class you're using won't be able to call plus/minus to add/subtract two of itself. For example... say your other derived class is 'foo':

1
2
3
4
5
6
class foo : public element
{  ... };

foo a, b;

foo c = a.plus(b);   // won't work 


That code won't work because plus doesn't return a foo, nor does it take foo as a parameter. It will only work with vectors, or if you want to add a vector to some other element and produce a vector:

1
2
3
4
foo a;
vector<int> b;

vector<int> c = a.plus(b);  // this is the only way this can work 


Given that... it really doesn't make any sense for that to be part of 'element'. It'd make much more sense for it to just be part of vector.

The whole point of polymorphism is that you can treat seperate types the same, without knowing the exact type. For example... a more reasonable plus function would take two element*s so you can do things like this

1
2
3
4
element* a = new /*either foo or vector, doesn't matter*/
element* b = new /* ditto */

a->plus(b);  // this should work no matter whether or not a or b are vectors/foos/a mix 


But even that is ill formed. What would plus return in that case?

Do you see why this doesn't really make sense?

I could wrap my head around this more if you could give some examples of other classes that derive from element. But right now it seems like element is just making trouble for yourself, and it doesn't seem like a good use of polymorphism at all.
i completly agree with you .
In class element i just added vector to see if that works with my vector class but you are right this is not how it should be.
class vector should use the plus function which add two vectors (x,y,z) this class is created by me so i am not using the library class.
another class called student will also use plus to add two lists of students list , the list contain struct of type student
so should i be using
element* a = new /*either foo or vector, doesn't matter*/
element* b = new /* ditto */
inside my element.h ?
I also tried something like this but not working inside element.h:
template <class T,class U> // changed from double template
virtual T plus(const U& vec);
Okay. This still doesn't make a lot of sense.

another class called student will also use plus to add two lists of students list


So I'm confused. How do you add two students together? And what's this about a list? Is 'student' really a list of students?

Let me ask you this... would you add a vector to a student? Like this:

1
2
3
4
5
6
student a;
vector b;

vector c = a.plus(b);  // this makes no sense to me..
//  how can you add a student and a vector together?
//  what would the result be? 


As my comment says, that makes no sense in my brain. student and vector are totally unrelated types, so I don't see how you would add them together like this.

Because this makes no sense, it makes no sense to do this polymophically. IE: your 'element' class serves no purpose. Why do you need to derive these unrelated types from it?


Let's look at this another way:

What is your desired behavior?

I'm still a little unclear on exactly what it is you're trying to accomplish with this element class. Maybe if I better understood what you're trying to do I could help more.

I know you're writing 'vector' and 'student' classes... but why do they have to be related?

And can you clarify what you meant by a list of students? Do you mean like a std::list?

I also tried something like this but not working inside element.h:
template <class T,class U> // changed from double template
virtual T plus(const U& vec);


That won't work because template functions can't be virtual.

Unfortunately, there's no quick fix to this problem because what you're trying to do is conceptually incorrect.
I am sorry if i am not clear in explaining the problem .
basically for class student when you add two so if u have list A + List B lets say type multimap because they will be indexed.plus function will add them together, thats it.
for vector class as i said it adds two vectors
Topic archived. No new replies allowed.