Creating custom class for vector of pairs

This is a discussion on Creating custom class for vector of pairs within the C++ Programming forums, part of the General Programming Boards category; I have the following code: Code: #include <iostream> #include <utility> using namespace std; template<typename T> class Vector { public: Vector(unsigned ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64

    Creating custom class for vector of pairs

    I have the following code:

    Code:
    #include <iostream>
    #include <utility>
    using namespace std;
    
    template<typename T>
    class Vector {
    	public:
    		Vector(unsigned int length = 10) {
    		}
    };
    
    template<typename P, typename Q>
    class Pair {
    
    };
    
    template<typename P, typename Q>
    class VectorOfPairs : public Vector<Pair<P, Q> > {
    	public:
    		VectorOfPairs(unsigned int length = 10) {
    			Vector<Pair<P, Q> >::Vector(length);
    		}
    };
    
    int main() {
    	VectorOfPairs<int, int> v;
    	return 0;
    }
    What I'm trying to do inside the "VectorOfPairs(unsigned int)" constructor is what we would write in Java as "super(length)". But for some reason, it gives me a compilation error:

    Code:
    test.cpp: In constructor `VectorOfPairs<P, Q>::VectorOfPairs(unsigned int) [with P = int, Q = int]':
    test.cpp:26:   instantiated from here
    test.cpp:21: error: dependent-name ` Vector<Pair<P, Q> >::Vector' is parsed as a non-type, but instantiation yields a type
    test.cpp:21: note: say `typename  Vector<Pair<P, Q> >::Vector' if a type is meant
    It seems to treat ` Vector<Pair<P, Q> >::Vector' as a type. But in reality, the type is ` Vector<Pair<P, Q> >', while the '::Vector' part is the constructor.

    So my question is: is there anyway to tell the compiler that Vector<Pair<P,Q> > is the type?
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You can't call a constructor like:
    Code:
    template<typename P, typename Q>
    class VectorOfPairs : public Vector<Pair<P, Q> > {
    	public:
    		VectorOfPairs(unsigned int length = 10) {
    			Vector<Pair<P, Q> >::Vector(length);
    		}
    };
    Try:
    Code:
    template<typename P, typename Q>
    class VectorOfPairs : public Vector<Pair<P, Q> > {
    	public:
    		VectorOfPairs(unsigned int length = 10) : Vector(length) {
    		}
    };

    But why don't you just use std::vector< std:air<int, int> > ?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Quote Originally Posted by EVOEx View Post
    You can't call a constructor like:
    Try:
    Code:
    template<typename P, typename Q>
    class VectorOfPairs : public Vector<Pair<P, Q> > {
    	public:
    		VectorOfPairs(unsigned int length = 10) : Vector(length) {
    		}
    };
    Actually, that would be "Vector<Pair<P, Q> >(length)" instead of "Vector(length)", apparently, since "Vector(length)" gives me a compilation error.

    Either way, thank you. But I'm curious: is there really no way to call the Vector constructor actually inside the VectorOfPairs constructor?

    Quote Originally Posted by EVOEx View Post
    But why don't you just use std::vector< std:air<int, int> > ?
    Because this is an assignment where we're supposed to make our own Vector and Pair classes. Sorry for not mentioning that earlier.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> is there really no way to call the Vector constructor actually inside the VectorOfPairs constructor?

    You can, but it won't initialize the base class. All base classes and member variables are initialized before the start of the constructor for your class. If you want to initialize them to a certain value or call a specific constructor for them, you must do so in the initializer list.

    In general, you should prefer to use the initializer list to initialize all member variables. You must use it to explicitly initialize base classes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Creating Custom Controls
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 12-29-2003, 12:41 PM
  5. custom class vector
    By -=SoKrA=- in forum C++ Programming
    Replies: 9
    Last Post: 07-08-2003, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21