c++ - Unable to instantiate templated class inside another class -
i have 2 classes: 1 templated, 1 not. trying create instance of templated class inside non-templated class , program won't compile. i'm using visual studio 2012 , error 'intellisense: expected type specifier' on line in bar.h:
foo<int> foo_complex(99); i can use syntax outside class (see console.cpp below). can use empty constructor inside class. gives? how correctly use non-empty constructor foo inside bar?
thanks in advance help. i've looked everywhere solution , come empty. example code below. class implementation inline clarity.
foo.h
#pragma once template<typename t> class foo { public: foo(); foo(int i); }; template<typename t> foo<t>::foo() { std::cout << "you created instance of foo without value." << std::endl; } template<typename t> foo<t>::foo(int i) { std::cout << "you created instance of foo int " << << std::endl; } bar.h
#pragma once #include "foo.h" class bar { private: foo<int> foo_simple; foo<int> foo_complex(99); // error ~ intellisense:expected type specifier public: bar(int i); }; bar::bar(int i) { std::cout << "you created instance of bar int " << << std::endl; } console.cpp
#include "stdafx.h" #include <iostream> #include <string> #include "foo.h" #include "bar.h" int _tmain(int argc, _tchar* argv[]) { foo<int> foo(1); bar bar(2); std::string = "any"; std::cout << std::endl; std::cout << "press key close window..." << std::endl; std::cin >> any; return 0; }
initialize member variables in constructor:
class bar { private: foo<int> foo_complex; public: bar(int i); }; bar::bar(int i) : foo_complex(99) { std::cout << "you created instance of bar int " << << std::endl; }
Comments
Post a Comment