Class and template questions

This is a discussion on Class and template questions within the C++ Programming forums, part of the General Programming Boards category; I've got a bit of code that looks something like this: Code: template <typename T> class A { private: class ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    43

    Question Class and template questions

    I've got a bit of code that looks something like this:
    Code:
    template <typename T>
    class A
    {
    private:
    	class B
    	{
    	public:
    		B() { count = 0 }
    		T data;
    	private:
    		unsigned int count;
    		friend class A;
    	};
    public:
    	A();
    	B* do_stuff(T some_data);
    };
    
    template <typename T>
    A<T>::B* A<T>::do_stuff(T some_data)
    {
    	B* temp = new B;
    	temp->data = some_data;
    	++(temp->count);
    	return temp;
    }
    The highlighted bit is where I'm stuck. I get the error:
    'A<T>::B' : dependent name is not a type
    I think I'm on the right track, but now I'm totally lost. Any advice?

    On a related note, in main() I'm looking to create a vector of B*. Is there any way of me doing that without having to declare the whole mess public? I want to do this:
    Code:
    int main()
    {
    	A<int> MyClass;
    	vector<A<int>::B*> my_vector;
    	for(int i = 0; i < 10; ++i)
    	{
    		my_vector.push_back(MyClass.do_stuff(i));
    	}
    	return 0;
    }
    However, I don't want to whole world access to anything inside of B. What am I forgetting to do?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Try "typename A<T>::B*" instead of "A<T>::B*" on the line in question
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    Ah, works like a charm. Thank you.

    Any ideas about the last part?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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