我有两个类:一个模板,一个不模板。 我试图在非模板化的类中创建模板化类的实例,程序将无法编译。 我正在使用Visual Studio 2012,我在bar.h中遇到错误'IntelliSense:expect a type specifier':
Foo<int> foo_complex(99);我可以在类之外使用此语法(请参阅下面的console.cpp)。 我可以在类中使用空构造函数。 是什么赋予了? 如何在Bar中正确使用Foo的非空构造函数?
在此先感谢您的帮助。 我到处寻找解决方案,然后空出来。 示例代码如下。 为清晰起见,类实现是内联的。
foo.h中
#pragma once template<typename T> class Foo { public: Foo(); Foo(int i); }; template<typename T> Foo<T>::Foo() { std::cout << "You created an instance of Foo without a value." << std::endl; } template<typename T> Foo<T>::Foo(int i) { std::cout << "You created an instance of Foo with int " << i << std::endl; }bar.h
#pragma once #include "foo.h" class Bar { private: Foo<int> foo_simple; Foo<int> foo_complex(99); // Error ~ IntelliSense:expected a type specifier public: Bar(int i); }; Bar::Bar(int i) { std::cout << "You created an instance of Bar with int " << i << 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 = "any"; std::cout << std::endl; std::cout << "Press any key to close this window..." << std::endl; std::cin >> any; return 0; }I have two classes: one templated, one not. I am trying to create an instance of the templated class inside the non-templated class and the program won't compile. I'm using Visual Studio 2012 and I get the error 'IntelliSense: expected a type specifier' on this line in bar.h:
Foo<int> foo_complex(99);I can use this syntax outside a class (see console.cpp below). I can use the empty constructor inside the class. What gives? How do I correctly use the non-empty constructor for Foo inside Bar?
Thanks in advance for your help. I've looked everywhere for a solution and come up empty. Example code is below. Class implementation is inline for 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 an instance of Foo without a value." << std::endl; } template<typename T> Foo<T>::Foo(int i) { std::cout << "You created an instance of Foo with int " << i << std::endl; }bar.h
#pragma once #include "foo.h" class Bar { private: Foo<int> foo_simple; Foo<int> foo_complex(99); // Error ~ IntelliSense:expected a type specifier public: Bar(int i); }; Bar::Bar(int i) { std::cout << "You created an instance of Bar with int " << i << 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 = "any"; std::cout << std::endl; std::cout << "Press any key to close this window..." << std::endl; std::cin >> any; return 0; }最满意答案
在构造函数中初始化成员变量:
class Bar { private: Foo<int> foo_complex; public: Bar(int i); }; Bar::Bar(int i) : foo_complex(99) { std::cout << "You created an instance of Bar with int " << i << std::endl; }Initialize member variables in the constructor:
class Bar { private: Foo<int> foo_complex; public: Bar(int i); }; Bar::Bar(int i) : foo_complex(99) { std::cout << "You created an instance of Bar with int " << i << std::endl; }无法在另一个类中实例化模板化类(Unable to instantiate templated class inside another class)我有两个类:一个模板,一个不模板。 我试图在非模板化的类中创建模板化类的实例,程序将无法编译。 我正在使用Visual Studio 2012,我在bar.h中遇到错误'IntelliSense:expect a type specifier':
Foo<int> foo_complex(99);我可以在类之外使用此语法(请参阅下面的console.cpp)。 我可以在类中使用空构造函数。 是什么赋予了? 如何在Bar中正确使用Foo的非空构造函数?
在此先感谢您的帮助。 我到处寻找解决方案,然后空出来。 示例代码如下。 为清晰起见,类实现是内联的。
foo.h中
#pragma once template<typename T> class Foo { public: Foo(); Foo(int i); }; template<typename T> Foo<T>::Foo() { std::cout << "You created an instance of Foo without a value." << std::endl; } template<typename T> Foo<T>::Foo(int i) { std::cout << "You created an instance of Foo with int " << i << std::endl; }bar.h
#pragma once #include "foo.h" class Bar { private: Foo<int> foo_simple; Foo<int> foo_complex(99); // Error ~ IntelliSense:expected a type specifier public: Bar(int i); }; Bar::Bar(int i) { std::cout << "You created an instance of Bar with int " << i << 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 = "any"; std::cout << std::endl; std::cout << "Press any key to close this window..." << std::endl; std::cin >> any; return 0; }I have two classes: one templated, one not. I am trying to create an instance of the templated class inside the non-templated class and the program won't compile. I'm using Visual Studio 2012 and I get the error 'IntelliSense: expected a type specifier' on this line in bar.h:
Foo<int> foo_complex(99);I can use this syntax outside a class (see console.cpp below). I can use the empty constructor inside the class. What gives? How do I correctly use the non-empty constructor for Foo inside Bar?
Thanks in advance for your help. I've looked everywhere for a solution and come up empty. Example code is below. Class implementation is inline for 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 an instance of Foo without a value." << std::endl; } template<typename T> Foo<T>::Foo(int i) { std::cout << "You created an instance of Foo with int " << i << std::endl; }bar.h
#pragma once #include "foo.h" class Bar { private: Foo<int> foo_simple; Foo<int> foo_complex(99); // Error ~ IntelliSense:expected a type specifier public: Bar(int i); }; Bar::Bar(int i) { std::cout << "You created an instance of Bar with int " << i << 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 = "any"; std::cout << std::endl; std::cout << "Press any key to close this window..." << std::endl; std::cin >> any; return 0; }最满意答案
在构造函数中初始化成员变量:
class Bar { private: Foo<int> foo_complex; public: Bar(int i); }; Bar::Bar(int i) : foo_complex(99) { std::cout << "You created an instance of Bar with int " << i << std::endl; }Initialize member variables in the constructor:
class Bar { private: Foo<int> foo_complex; public: Bar(int i); }; Bar::Bar(int i) : foo_complex(99) { std::cout << "You created an instance of Bar with int " << i << std::endl; }
发布评论