首页 > 其他分享 >[Error] a template declaration cannot appear at block scope

[Error] a template declaration cannot appear at block scope

时间:2023-02-13 12:44:55浏览次数:36  
标签:appear nums namespace cannot Vec template Error using include

测试 C++11中  Alias Template 时出现出现报错

程序:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	template <typename T>
	using Vec = vector<T>; 
	Vec<int> nums;
	nums.push_back(1);
	
	return 0;
}

报错:

 

 查阅相关资料,发现 C++标准有:

Template declarations are only permitted at global, namespace, or class scope.

表示 template 只能在全局,命名空间和类内声明。程序错误的将其声明在了函数内部。

修改为:

#include <iostream>
#include <vector>
using namespace std;

template <typename T>
using Vec = vector<T>;
	
int main()
{
	Vec<int> nums;
	nums.push_back(1);
	
	return 0;
}

  

标签:appear,nums,namespace,cannot,Vec,template,Error,using,include
From: https://www.cnblogs.com/kyzh-lhl/p/17115761.html

相关文章