别名使用vs typedef

alias using vs typedef

本文关键字:typedef vs 别名      更新时间:2023-10-16

我在学校实验室工作,说明书上写着:

将定义Word_List的typedef更改为别名声明(使用)

根据我在谷歌上搜索到的内容,这样做的方法是从

typedef vector<Word_Entry> Word_List;

至:

using Word_List = std::vector<Word_Entry>;

但是当我编译时,我得到以下错误:

error: expected nested-name-specifier before 'Word_List'

大部分代码:

#include <iostream>
#include <algorithm>
#include <list>
#include <string>
#include <vector>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;
struct Word_Entry
{
  string ord;
  unsigned cnt;
};
//typedef vector<Word_Entry> Word_List; <-This is what it used to look like
using Word_List = vector<Word_Entry>;

int main()
{
 ...
}

传统信息:

Yes, I am compiling with c++11 
Word_Entry is a struct that stores 2 variables
I have the line "using namespace std;" in the begining of my code
I'm using mingw compiler

您可以在这里看到解决方案:

#include <string>
#include <vector>
using namespace std;
struct Word_Entry
{
  string ord;
  unsigned cnt;
};
//typedef vector<Word_Entry> Word_List; <-This is what it used to look like
using Word_List = vector<Word_Entry>;

int main()
{
}
  • http://ideone.com/9mCaq0(C++)错误:"Word_List"之前应有嵌套的名称说明符
  • http://ideone.com/FdIKcr(C++11)成功

您有一个配置错误,您没有使用C++11规范进行编译。