在模板,在那里,为什么我必须把typename和template上依赖的名字呢?究竟什么是依赖名称?我有以下代码:
template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
// ...
template<typename U> struct inUnion {
// Q: where to add typename/template here?
typedef Tail::inUnion<U> dummy;
};
template< > struct inUnion<T> {
};
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
// ...
template<typename U> struct inUnion {
char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …Run Code Online (Sandbox Code Playgroud) 此代码编译并按预期工作(它在运行时抛出,但没关系):
#include <iostream>
#include <boost/property_tree/ptree.hpp>
void foo(boost::property_tree::ptree &pt)
{
std::cout << pt.get<std::string>("path"); // <---
}
int main()
{
boost::property_tree::ptree pt;
foo(pt);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是只要我添加模板并将foo原型更改为
template<class ptree>
void foo(ptree &pt)
Run Code Online (Sandbox Code Playgroud)
我在GCC中遇到错误:
test_ptree.cpp: In function ‘void foo(ptree&)’:
test_ptree.cpp:7: error: expected primary-expression before ‘>’ token
Run Code Online (Sandbox Code Playgroud)
但MSVC++没有错误!错误在标记的行中<---.再次,如果我将问题线改为
--- std::cout << pt.get<std::string>("path"); // <---
+++ std::cout << pt.get("path", "default value");
Run Code Online (Sandbox Code Playgroud)
错误消失(问题是明确的<std::string>).
Boost.PropertyTree需要Boost> = 1.41.请帮助我理解并修复此错误.
请参阅模板:模板功能与类的模板成员函数不兼容 - 一个类似的流行问题包含其他好的答案和解释.
刚才我不得不浏览网站,找出为什么模板类模板成员函数给出了语法错误:
template<class C> class F00 {
template<typename T> bar();
};
...
Foo<C> f;
f.bar<T>(); // syntax error here
Run Code Online (Sandbox Code Playgroud)
我现在意识到模板括号被视为关系运算符.要执行预期的操作,需要以下奇怪的语法,cf 模板:模板函数与类的模板成员函数不兼容:
f.template bar<T>();
Run Code Online (Sandbox Code Playgroud)
您遇到的C++/C++模板的其他奇怪方面和问题是不是您认为是常识的东西?
我已经看过一些相关的 帖子,但是我无法理解我需要做些什么来修复我正在为入门级C++课程制作的程序.
我的错误是:
Build Final Project of project Final Project with configuration Debug
Ld "build/Debug/Final Project" normal x86_64
cd "/Users/nick/Dropbox/|Syncs/Xcode/Final Project"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk "- L/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug" "-F/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug" -filelist "/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Final Project.build/Debug/Final Project.build/Objects-normal/x86_64/Final Project.LinkFileList" -mmacosx-version-min=10.6 -o "/Users/nick/Dropbox/|Syncs/Xcode/Final Project/build/Debug/Final Project"
Undefined symbols:
"Vector<double>::Vector()", referenced from:
_main in main.o
"Vector<double>::length()", referenced from:
_main in main.o
"Vector<double>::Vector(double const&, double const&, double const&)", referenced from:
_main in main.o
_main in main.o
"Vector<double>::getx() const", referenced from:
_main …Run Code Online (Sandbox Code Playgroud) 可能重复:
模板:模板功能与类的模板成员函数不兼容
template <typename T>
struct A
{
template <int I>
void f();
};
template <typename T>
void F(A<T> &a)
{
a.f<0>(); // expected primary-expression before ‘)’ token
}
int main()
{
A<int> a;
a.f<0>(); // This one is ok.
}
Run Code Online (Sandbox Code Playgroud)
这是什么一回事?