error: expected primary-expression before 'template' - what am I doing wrong?
error: expected primary-expression before 'template' - what am I doing wrong?
#include <iostream>
#include <vector>
template<class T> struct A {
A( const std::initializer_list<T> &list ) {
for( template std::initializer_list<T>::iterator it = list.begin();
it != list.end(); ++it ) {
content.emplace( *it );
}
}
~A() {};
std::vector<T> content;
};
int main() {
A<int> a = A<int>( { 1, 2, 3, 4, 5 } );
for( const int x : a.content ) {
std::cout << x << " ";
}
return 0;
}
上面的代码反映了我遇到的问题。当我尝试编译时,我得到:
error: expected primary-expression before 'template'...
和
error: 'it' was not declared in this scope
对于第 14 行(迭代器 for
循环)。我尝试了一些变体,但我对它们一无所获。有人知道这个问题吗?谢谢
模板关键字使用不正确,假设是typename
或者直接使用auto
for(模板std::initializer_list::iterator它=list.begin();
^^^^^^^^ != list.end(); ++它){
使用不正确[std::vector::emplace][1]
修复方法是:
for( auto it = list.begin(); it != list.end(); ++it )
{
content.emplace(content.end(), *it );
}
或:
content.insert(content.end(), list);
把你定义的it
前面的template
去掉
for (std::initializer_list<T>::iterator it = list.begin();
it != list.end(); ++it) {
content.emplace(*it);
}
你也用错了emplace函数。 Here 是 emplace
的文档
#include <iostream>
#include <vector>
template<class T> struct A {
A( const std::initializer_list<T> &list ) {
for( template std::initializer_list<T>::iterator it = list.begin();
it != list.end(); ++it ) {
content.emplace( *it );
}
}
~A() {};
std::vector<T> content;
};
int main() {
A<int> a = A<int>( { 1, 2, 3, 4, 5 } );
for( const int x : a.content ) {
std::cout << x << " ";
}
return 0;
}
上面的代码反映了我遇到的问题。当我尝试编译时,我得到:
error: expected primary-expression before 'template'...
和
error: 'it' was not declared in this scope
对于第 14 行(迭代器 for
循环)。我尝试了一些变体,但我对它们一无所获。有人知道这个问题吗?谢谢
模板关键字使用不正确,假设是
typename
或者直接使用auto
for(模板std::initializer_list::iterator它=list.begin(); ^^^^^^^^ != list.end(); ++它){
使用不正确
[std::vector::emplace][1]
修复方法是:
for( auto it = list.begin(); it != list.end(); ++it )
{
content.emplace(content.end(), *it );
}
或:
content.insert(content.end(), list);
把你定义的it
template
去掉
for (std::initializer_list<T>::iterator it = list.begin();
it != list.end(); ++it) {
content.emplace(*it);
}
你也用错了emplace函数。 Here 是 emplace